05 August 2008
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
1
1-9
without repetition.1
1-9
without repetition.1
3x3
sub-boxes of the grid must contain the digits 1
1-9
without repetition.
A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character
.1
'.'
Example 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
Input:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: true
Example 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:
1
1-9
and the character 1
'.'
.1
9x9
.一个数独表里面已经填了一些数字,没填数字的cell用’.’表示,检查目前这个数独表是否有效。检查的方式是是行,列和3X3的小方块,对二维数组进行遍历,遍历每一行的时候,新建3个HashSet,来检查该行,该行/该列和该cube对应的set是否含有数字,注意,HashSet的add()方法是有返回值的。
3个HashSets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public boolean isValidSudoku(char[][] board) {
if (board == null || board.length == 0 || board[0].length == 0) {
return false;
}
int m = board.length;
int n = board[0].length;
for (int i = 0; i < m; i++) {
HashSet<Character> rows = new HashSet<>();
HashSet<Character> cols = new HashSet<>();
HashSet<Character> cube = new HashSet<>();
for (int j = 0; j < n; j++) {
char chRow = board[i][j];
//判断current字符是否不在1~9范围之内
if (chRow != '.' && (chRow < '1' || chRow > '9')) {
return false;
}
//check row
if (chRow != '.' && !rows.add(chRow)) {
return false;
}
//check col
char chCol = board[j][i];
if (chCol != '.' && !cols.add(chCol)) {
return false;
}
// 得到cube中的坐標
int cubeIndexX = 3 * (i / 3) + j / 3;
int cubeIndexY = 3 * (i % 3) + j % 3;
//check the cube
if (board[cubeIndexX][cubeIndexY] != '.' && !cube.add(board[cubeIndexX][cubeIndexY])) {
return false;
}
}
}
return true;
}
}
3个hashMaps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public boolean isValidSudoku(char[][] board) {
// init data
HashMap<Integer, Integer> [] rows = new HashMap[9];
HashMap<Integer, Integer> [] columns = new HashMap[9];
HashMap<Integer, Integer> [] boxes = new HashMap[9];
for (int i = 0; i < 9; i++) {
rows[i] = new HashMap<Integer, Integer>();
columns[i] = new HashMap<Integer, Integer>();
boxes[i] = new HashMap<Integer, Integer>();
}
// validate a board
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char num = board[i][j];
if (num != '.') {
int n = (int)num;
int box_index = (i / 3 ) * 3 + j / 3;
// keep the current cell value
rows[i].put(n, rows[i].getOrDefault(n, 0) + 1);
columns[j].put(n, columns[j].getOrDefault(n, 0) + 1);
boxes[box_index].put(n, boxes[box_index].getOrDefault(n, 0) + 1);
// check if this value has been already seen before
if (rows[i].get(n) > 1 || columns[j].get(n) > 1 || boxes[box_index].get(n) > 1)
return false;
}
}
}
return true;
}
}