Java Solution without using HashSet


  • 0
    X
    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            //row
            for (int i = 0; i < board.length; i++) {
                boolean[] array = new boolean[9];
                for (int j = 0; j < board[0].length; j++) {
                    if (board[i][j] != '.') {
                        if (array[board[i][j]-'1']) {
                            return false;
                        }
                        array[board[i][j]-'1'] = true;
                    }
                }
            }
            //column
            for (int j = 0; j < board[0].length; j++) {
                boolean[] array = new boolean[9];
                for (int i = 0; i < board.length; i++) {
                    if (board[i][j] != '.') {
                        if (array[board[i][j]-'1']) {
                            return false;
                        }
                        array[board[i][j]-'1'] = true;
                    }
                }
            }
            //block
            for (int k = 0; k < board.length; k++) {
                int rowStart = (k / 3) * 3;
                int colStart = (k % 3) * 3;
                boolean[] array = new boolean[9];
                for (int i = rowStart; i < rowStart + 3; i++) {
                    for (int j = colStart; j < colStart + 3; j++) {
                        if (board[i][j] != '.') {
                            if (array[board[i][j]-'1']) {
                                return false;
                            }
                            array[board[i][j]-'1'] = true;
                        }
                    }
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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