Why code run correctly in my PC but not pass?


  • 0
    X

    static short line[2][9];
    static short block[3][3];
    short mask[] = {1,2,4,8,16,32,64,128,256};

    for(int i=0; i<9; ++i)
        for(int j=0; j<9; ++j){
            if(board[i][j] == '.')
                continue;
            
            int k = board[i][j]-'1';
            
            if(k<0 || k>8) return false;
            
            if(line[0][i]&mask[k])
                return false;
            else 
                line[0][i] |= mask[k];
                
            if(line[1][j]&mask[k])
                return false;
            else 
                line[1][j] |= mask[k];
                
            if(block[i/3][j/3]&mask[k])
                return false;
            else 
                block[i/3][j/3] |= mask[k];
        }
        
    return true;

  • 1
    C

    Your use of static should be inverted.




    mask can be static because it won't change across multiple validation of boards. But line and block must be initialized each time. Otherwise it will "remember" the last validated board and results will be wrong from 2nd board on wards.


    Here is simple correction. I confirmed that your solution got accepted with this. Congratulations !!

    // static short line[2][9]; static short block[3][3]; short mask[] = {1,2,4,8,16,32,64,128,256};
    // make mask static but init. line and block every time!
    static short mask[] = {1,2,4,8,16,32,64,128,256};
    short line [2][9] = { 0 } ;
    short block[3][3] = { 0 } ;
    

  • 0
    X

    thanks,it works!


Log in to reply
 

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