What' wrong with my solution?


  • 0
    W

    I got the result of runtime error with a very very long testcase. Could anyone help me with my solution?
    Thanks.

    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:
                return
            rowLen = len(board)
            colLen = len(board[0])
            
            def checkIsland(row, col):
                board[row][col] = "A"
                around = [(1,0),(-1,0),(0,1),(0,-1)]
                for x, y in around:
                    nRow = row + x
                    nCol = col + y
                    if 0 <= nRow < rowLen and 0 <= nCol < colLen and board[nRow][nCol] == "O":
                        checkIsland(nRow,nCol)
            
            for row in xrange(rowLen):
                if board[row][0] == "O":
                    checkIsland(row, 0)
                if board[row][colLen - 1] == "O":
                    checkIsland(row, colLen - 1)
            for col in xrange(colLen):
                if board[0][col] == "O":
                    checkIsland(0, col)
                if board[-1][col] == "O":
                    checkIsland(rowLen - 1, col)
            for row in xrange(rowLen):
                for col in xrange(colLen):
                    if board[row][col] == "A":
                        board[row][col] = "O"
            return

Log in to reply
 

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