share my python version

  • 0

    Re: [Java DFS + boundary cell turning solution](simple and clean code)

    class Solution(object):
        def solve(self, board):
            :type board: List[List[str]]
            :rtype: void Do not return anything, modify board in-place instead.
            if not board or len(board) < 2 or len(board[0]) < 2:
            m, n = len(board), len(board[0])
            for i in xrange(m):
                if board[i][0] == 'O' : self.dfs(board, i, 0)
                if board[i][n-1] == 'O': self.dfs(board, i, n-1)
            for j in xrange(n):
                if board[0][j] == 'O': self.dfs(board, 0, j)
                if board[m-1][j] == 'O': self.dfs(board, m-1, j)
            for i in xrange(m):
                for j in xrange(n):
                    if board[i][j] == 'O': board[i][j] = 'X'
                    elif board[i][j] == '*': board[i][j] = 'O'
        def dfs(self, board, i, j):
            if board[i][j] != 'O': 
            board[i][j] = '*'
            if i > 1:
                self.dfs(board, i-1, j)
            if j > 1:
                self.dfs(board, i, j-1)
            if i < len(board) - 2:
                self.dfs(board, i+1, j)
            if j < len(board[0]) - 2:
                self.dfs(board, i, j+1)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.