本文主要是介绍Python | Leetcode Python题解之第130题被围绕的区域,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution:def solve(self, board: List[List[str]]) -> None:if not board:returnn, m = len(board), len(board[0])que = collections.deque()for i in range(n):if board[i][0] == "O":que.append((i, 0))board[i][0] = "A"if board[i][m - 1] == "O":que.append((i, m - 1))board[i][m - 1] = "A"for i in range(m - 1):if board[0][i] == "O":que.append((0, i))board[0][i] = "A"if board[n - 1][i] == "O":que.append((n - 1, i))board[n - 1][i] = "A"while que:x, y = que.popleft()for mx, my in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:if 0 <= mx < n and 0 <= my < m and board[mx][my] == "O":que.append((mx, my))board[mx][my] = "A"for i in range(n):for j in range(m):if board[i][j] == "A":board[i][j] = "O"elif board[i][j] == "O":board[i][j] = "X"
这篇关于Python | Leetcode Python题解之第130题被围绕的区域的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!