码迷,mamicode.com
首页 > 其他好文 > 详细

leetccode-130-被围绕的区域

时间:2019-07-16 12:53:52      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:range   bsp   any   src   dfs   nbsp   并查集   list   区域   

题目描述:

技术图片

方法一: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-被围绕的区域

标签:range   bsp   any   src   dfs   nbsp   并查集   list   区域   

原文地址:https://www.cnblogs.com/oldby/p/11194038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!