Leetcode 130. 被围绕的区域 (每日一题 20210720 同类型题)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 130. 被围绕的区域 (每日一题 20210720 同类型题)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
'O'?相連的?'O'?最終都會(huì)被填充為?'X'。如果兩個(gè)元素在水平或垂直方向相鄰,則稱它們是“相連”的。
示例 2:輸入:board = [["X"]]
輸出:[["X"]]提示:m == board.length
n == board[i].length
1 <= m, n <= 200
board[i][j] 為 'X' 或 'O'鏈接:https://leetcode-cn.com/problems/surrounded-regionsclass Solution:def solve(self, board: List[List[str]]) -> None:class Solution:def solve(self, borad: List[List[str]]) -> None:row, col = len(borad), len(board[0])def dfs(x, y):if board[x][y] == 'X':return else:board[x][y] = '#'for c in [(0,1),(0,-1),(-1,0),(-1,1)]:nx, ny = x + c[0], y + c[1]if 0 <= nx < row and 0 <= ny < col:dfs(nx, ny)for i in range(row):dfs(i, 0)dfs(i, col-1)for j in range(1, col-1):dfs(0, j)dfs(row-1, j)for i in range(row):for j in range(col):if board[x][y] == '#':board[x][y] = 'O'else:board[x][y] = 'X'
總結(jié)
以上是生活随笔為你收集整理的Leetcode 130. 被围绕的区域 (每日一题 20210720 同类型题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Leetcode 79. 单词搜索 (每
- 下一篇: Leetcode 64 最小路径和 (每