Java O(n) solution with arrays, 27 ms, beats 94.91%


  • 0
    A

    where n is the sum number of elements in 2-dimensional array

    public boolean isValidSudoku(char[][] board) {
            boolean[][] rows = new boolean[9][9];
            boolean[][] columns = new boolean[9][9];
            boolean[][] squares = new boolean[9][9];
            for (int i = 0; i < board.length; i++) {
              for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == '.') {
                  continue;
                }
                int num = board[i][j] - '1';
                if (rows[i][num]) {
                  return false;
                }
                rows[i][num] = true;
                if (columns[j][num]) {
                  return false;
                }
                columns[j][num] = true;
                if (squares[(i / 3) * 3 + j / 3][num]) {
                  return false;
                }
                squares[(i / 3) * 3 + j / 3][num] = true;
              }
            }
            return true;
        }
    
    

Log in to reply
 

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