Could you help to take a look at my code

  • 0

    I have got run time error with the very long test vector during submit. However, when I run it individually it is OK.
    Here is my code:

    class Solution {
        void travel(vector<vector<char>>& board, int x, int y, int nrow, int ncol) {
            if(board[x][y]!='O') return;
            board[x][y] = '1';
            if(x-1>=0 && x-1<nrow) travel(board, x-1, y, nrow, ncol);
            if(x+1>=0 && x+1<nrow) travel(board, x+1, y, nrow, ncol);
            if(y-1>=0 && y-1<ncol) travel(board, x, y-1, nrow, ncol);
            if(y+1>=0 && y+1<nrow) travel(board, x, y+1, nrow, ncol);
        void solve(vector<vector<char>>& board) {
            int nrow = board.size(), ncol = (nrow>0)?board[0].size():0;
            if(nrow<3 || ncol<3) return;
            for(int x = 0; x<nrow; x++) {
                travel(board, x, 0, nrow, ncol);
                travel(board, x, ncol-1, nrow, ncol);
            for(int y=1; y<ncol-1; y++) {
                travel(board, 0, y, nrow, ncol);
                travel(board, nrow-1, y, nrow, ncol);
            for(int x=0; x<nrow;x++) {
                for(int y=0; y<ncol; y++) {
                    if(board[x][y]=='1') board[x][y]='O';
                    else if(board[x][y]=='O') board[x][y]='X';

Log in to reply

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