Human-readable Java Solution


  • 0
    V
    public boolean isValidSudoku(char[][] board) {
        List<Set<Integer>> rowSetList = new ArrayList<>();
        List<Set<Integer>> columnSetList = new ArrayList<>();
        List<Set<Integer>> blockSetList = new ArrayList<>();
        int l = board.length;
        for (int i = 0; i < l; i++) {
            rowSetList.add(new HashSet<>());
            columnSetList.add(new HashSet<>());
            blockSetList.add(new HashSet<>());
        }
    
        for (int i = 0; i < l; i++) {
            for (int j = 0; j < l; j++) {
                if (board[i][j] == '.')continue;
                if (board[i][j] > '9' || board[i][j] <'1') return false;
                if (rowSetList.get(i).contains(board[i][j] -'0')) return false;
                else rowSetList.get(i).add(board[i][j] -'0');
                if (columnSetList.get(j).contains(board[i][j] -'0')) return false;
                else columnSetList.get(j).add(board[i][j] -'0');
                if (blockSetList.get(i/3+ (j/3)*3).contains(board[i][j] - '0')) return false;
                else blockSetList.get(i/3+ (j/3)*3).add(board[i][j] -'0');
    
            }
        }
        return true;
    }

Log in to reply
 

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