Java solution easy to understand


  • 13
    M
    public boolean isValidSudoku(char[][] board) {
        boolean[][] col = new boolean[9][9];
        boolean[][] row = new boolean[9][9];
        boolean[][] block = new boolean[9][9];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] == '.') {
                    continue;
                }
                int digit = board[i][j] - '1';
                int blockId = i / 3 * 3 + j / 3;
                if (col[j][digit]) {
                    return false;
                } else {
                    col[j][digit] = true;
                }
                if (row[i][digit]) {
                    return false;
                } else {
                    row[i][digit] = true;
                }
                if (block[blockId][digit]) {
                    return false;
                } else {
                    block[blockId][digit] = true;
                }
            }
        }
        return true;
    }

  • 0
    T

    I implemented it the same way but removed the else statements and put all the "= true" statements at the end to make it a bit more readable


Log in to reply
 

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