My easy JAVA solution beat 98% with concise thinking!very easy


  • 2
    T
     public boolean isValidSudoku(char[][] board) {
            boolean[][] row=new boolean[9][9];
            boolean[][] column=new boolean[9][9];
            boolean[][] cube=new boolean[9][9];
            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++){
                    if(board[i][j]=='.')continue;
                    int loc=board[i][j]-'1';
                    if(row[i][loc]||column[j][loc]||cube[(i/3)*3+j/3][loc])return false;
                    row[i][loc]=true;
                    column[j][loc]=true;
                    cube[(i/3)*3+j/3][loc]=true;
                }
            }
            return true;
        }
    

    just a label transfer for cube ; number=(i/3)*3+(j/3);then go on very easy to think


  • 0

    Excellent solution! This code only scan the table once.
    Just a little explanation about cube transfer:
    the reason to use (i/3)*3 is that,
    when i = 0,1,2, the value of this equation is 0,
    when i = 3,4,5, the value of this equation is 3,
    when i = 6,7,8, the value of this equation is 6.
    Division can cut i into 3 slots, plus j, it pinpoints the location in Boolean 2D array.


Log in to reply
 

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