Very easy Java Solution using Array to simulate dictionary


  • 5
    public boolean isValidSudoku(char[][] board) {
        int[][] rowDicts = new int[9][9];
        int[][] colDicts = new int[9][9];
        int[][] cellDicts = new int[9][9];
        
        for(int r=0; r<9; r++) {
            for(int c=0; c<9; c++) {
                if(board[r][c]!='.') {
                    int num = Integer.parseInt(""+board[r][c]);
                    if(rowDicts[r][num-1]==1 || 
                       colDicts[c][num-1]==1 ||
                       cellDicts[(r/3)*3+(c/3)][num-1]==1) {
                        return false;
                    } else {
                       rowDicts[r][num-1] = 1;
                       colDicts[c][num-1] = 1;
                       cellDicts[(r/3)*3+(c/3)][num-1] = 1;
                    }
                }
            }
        }
        
        return true;
    }

Log in to reply
 

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