I got different result comparing with my local machine


  • 0
    C

    This is my code. It is quite straight forward.
    The result from leetcode with my local one

    class Solution {
    public:
        void solve(vector<vector<char> > &board) {
            if(board.size()<3||board[0].size()<3)
                return;
            vector<vector<char> > cpboard(board.size(),vector<char>(board[0].size(),'X'));
           
            int row=board.size()-1;
            int col=board[0].size()-1;
            for(int i=0;i<board[0].size();i++){
                if(board[0][i]=='O'){
                    val(0,i,board,cpboard);
                }
                if(board[row][i]=='O'){
                    val(row,i,board,cpboard);
                }
            }
            for(int i=1;i<row;i++){
                if(board[i][0]=='O')
                    val(i,0,board,cpboard);
                if(board[i][col]=='O')
                    val(i,col,board,cpboard);
            }
            board.clear();
            board=cpboard;
           
            return;
        }
    
        void val(const int& row, const int& col, vector<vector<char> > &board, vector<vector<char> > &cpboard){
            static int rowmax=board.size()-1;
            static int colmax=board[0].size()-1;
            int index=row*board[0].size();
            
            cpboard[row][col]='O';
    
            if(row>0){
                if(board[row-1][col]=='O')
                    if(cpboard[row-1][col]=='X'){
                        val(row-1,col,board,cpboard);
                    }
            }
            if(row<rowmax){
                if(board[row+1][col]=='O')
                    if(cpboard[row+1][col]=='X'){
                        val(row+1,col,board,cpboard);
                    }
            }
            if(col>0){
                if(board[row][col-1]=='O')
                    if(cpboard[row][col-1]=='X'){
                        val(row,col-1,board,cpboard);
                    }
            }
            if(col<colmax){
                if(board[row][col+1]=='O')
                    if(cpboard[row][col+1]=='X'){
                        val(row,col+1,board,cpboard);
                    }
            }
        }
    };
    

  • 0
    S

    static might be the problem.


  • 0
    C

    Yes, you are right! that is the problem! Thank you so much!


  • 0
    C

    Yes, you are right! that is the problem! Thank you so much! And would you be so kind to tell me why the keyword "static" could results in such problems?


  • 0
    S

    Because OJ run your code once to test all testcase. It would be better to post code with some detail explanation for asking question in LeetCode Discuss. That is why your post is closed.


Log in to reply
 

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