leetccode-130-被围绕的区域

Posted oldby

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetccode-130-被围绕的区域相关的知识,希望对你有一定的参考价值。

题目描述:

技术图片

方法一:dfs

class Solution:
    def solve(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        if not board or len(board)<=2 or len(board[0])<=2:
            return
        row = len(board)
        col = len(board[0])
        def dfs(i,j):
            if i < 0 or j < 0 or i >= row or j >= col or board[i][j] != O:
                return
            board[i][j] = #
            dfs(i-1,j)
            dfs(i+1,j)
            dfs(i,j+1)
            dfs(i,j-1)
            
        for i in range(row):
            dfs(i,0)
            dfs(i,col-1)
            
        for j in range(col):
            dfs(0,j)
            dfs(row-1,j)
        for i in range(0, row): 
            for j in range(0, col): 
                if board[i][j] == O: 
                    board[i][j] = X 
                if board[i][j] == #: 
                    board[i][j] = O

方法二:并查集

 

以上是关于leetccode-130-被围绕的区域的主要内容,如果未能解决你的问题,请参考以下文章

lintcode:被围绕的区域

477. 被围绕的区域

题目地址(130. 被围绕的区域)

LeetCode 被围绕的区域

LeetCode 130. 被围绕的区域

130. 被围绕的区域