Why stack overflow?


  • 0
    D
    public class Solution {
        public void solve(char[][] board) {
            int row = board.length;
            if(row==0) return;
            int col = board[0].length;
            for(int i=0; i<col; i++)
                if(board[0][i]=='O') dfs(board, 0, i, row, col);
            for(int i=0; i<col; i++)
                if(board[row-1][i]=='O') dfs(board, row-1, i, row, col);
            for(int i=0; i<row; i++)
                if(board[i][0]=='O') dfs(board, i, 0, row, col);
            for(int i=0; i<row; i++)
                if(board[i][col-1]=='O') dfs(board, i, col-1, row, col);
            for(int i=0; i<row; i++){
                for(int j=0; j<col; j++){
                    if(board[i][j]=='O') board[i][j] = 'X';
                    if(board[i][j]=='S') board[i][j] = 'O';
                }
            }
        }
        private void dfs(char[][] board, int i, int j, int row, int col){
            if(i<0||i>=row||j<0||j>=col||board[i][j]!='O') return;
            board[i][j] = 'S';
            dfs(board, i-1, j, row, col);
            dfs(board, i+1, j, row, col);
            dfs(board, i, j-1, row, col);
            dfs(board, i, j+1, row, col);
        }
    }
    

Log in to reply
 

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