4ms clear java code, needs some advices to be better


  • 0
    J
    public boolean isValidSudoku(char[][] board) {
            if(board == null) return true;
            int size = board.length;
            int check = 0;
            
            //row check
            for(int i = 0; i < size; i++){
                check = 0;
                for(int j = 0; j < size; j++){
                    if(board[i][j]!='.'){
                        if((check & (1<<(board[i][j]-1))) != 0){
                            return false;
                        }
                        check = check | (1<<(board[i][j]-1));
                    }
                }
            }
            
            //column check
            for(int i = 0; i < size; i++){
                check = 0;
                for(int j = 0; j < size; j++){
                    if(board[j][i]!='.'){
                        if((check & (1<<(board[j][i]-1))) != 0){
                            return false;
                        }
                        check = check | (1<<(board[j][i]-1));
                    }
                }
            }
            
            //square check
            for(int row = 0; row < 9; row+=3){
                for(int col = 0; col < 9; col+=3){
                    check=0;
                    for(int i = row; i < 3 + row; i++){
                        for(int j = col; j < 3 + col; j++){
                            if(board[i][j]!='.'){
                                if((check & (1<<(board[i][j]-1))) != 0){
                                    return false;
                                }
                                check = check | (1<<(board[i][j]-1));
                            }
                        }
                    }
                }
            }
            return true;
        }

Log in to reply
 

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