easy C++ Solution. Runtime error on OJ?


  • 0
    K
    class Solution {
    public:
        int m,n;
        void solve(vector<vector<char>>& board) {
            if(board.empty()) return;
            m=board.size();
            n=board[0].size();
            for(int i=0;i<m;++i){
                fill(board,i,0);
                fill(board,i,n-1);
            } 
            for(int i=0;i<n;++i){
                fill(board,0,i);
                fill(board,m-1,i);
            }
            for(int i=0;i<m;++i)
                for(int j=0;j<n;++j){
                    if(board[i][j]=='O') board[i][j]='X';
                    if(board[i][j]=='S') board[i][j]='O';
                }
        }
        void fill(vector<vector<char>>& board,int r,int c){
            if(r<0 || r>=m || c<0 || c>=n) return;
            if(board[r][c]!='O') return;
            board[r][c]='S';
            fill(board,r-1,c);
            fill(board,r+1,c);
            fill(board,r,c-1);
            fill(board,r,c+1);
        }
    };

Log in to reply
 

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