Why my solutions is stackoverflow?


  • 0
    S

    It is exactly the same solution with others. Could somebody tell me what's wrong?
    Thanks a lot!

    public class Solution {
        public void solve(char[][] board) {
            if(board.length == 0 || board[0].length == 0) return;
            for(int i = 0; i < board.length - 1; i++){
                if(board[i][0] == 'O')
                    test(board, 0, i);
                if(board[i][board[0].length - 1] == 'O')
                    test(board, i, board[0].length - 1);
            }
            for(int i = 0; i < board[0].length; i++){
                if(board[0][i] == 'O')
                    test(board, 0, i);
                if(board[board.length - 1][i] == 'O')
                    test(board, board.length - 1, i);
            }
            for(int i = 0; i < board.length; i++){
                for(int j = 0; j < board[0].length; j++){
                    if(board[i][j] == 'O')
                        board[i][j] = 'X';
                    if(board[i][j] == '1')
                        board[i][j] = 'O';
                }
            }
        }
        
        private void test(char[][] b, int x, int y){
            b[x][y] = '1';
            if(x - 1 >= 0 && b[x - 1][y] == 'O')
                test(b, x - 1, y);
            if(x + 1 < b.length && b[x + 1][y] == 'O')
                test(b, x + 1, y);
            if(y - 1 >= 0 && b[x][y - 1] == 'O')
                test(b, x, y - 1);
            if(y + 1 < b[0].length && b[x][y + 1] == 'O')
                test(b, x, y + 1);
        }
    }
    

Log in to reply
 

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