Description:
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
1-9
without repetition. - Each column must contain the digits
1-9
without repetition. - Each of the 9
3x3
sub-boxes of the grid must contain the digits1-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 '.'
. Example 1:
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:
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:
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
- The given board contain only digits
1-9
and the character'.'
. - The given board size is always
9x9
.
解法:
此题无需判断数独是否有解,只需要保证3个基本条件,即每一行的元素不重复,每一列的元素不重复,每一个子格的元素不重复。对于重复的判断,借用数据结构Map(因为Map的Key不允许重复)。
整体上来看,判断是否重复的对象为9行,9列,9个子格,故外层需循环9次。而内层循环针对外层循环的内部元素进行循环。对于行,列,子格,其内部都有9个最小单位元素。
对于行而言,第i行对应的元素为1
board[i][j]
对于列而言,第i列对应的元素为1
board[j][i]
对于子格元素的对应
观察行号规律:
第0个九宫格:000111222; 第1个九宫格:000111222; 第2个九宫格:000111222;
第3个九宫格:333444555; 第4个九宫格:333444555; 第5个九宫格:333444555;
第6个九宫格:666777888; 第7个九宫格:666777888; 第8个九宫格:666777888;
可见对于每三个九宫格行号增3;对于单个九宫格,每三个格点行号增1。
因此第i个九宫格的第j个格点的行号可表示为i/3*3+j/3
观察列号规律:
第0个九宫格:012012012; 第1个九宫格:345345345; 第2个九宫格:678678678;
第3个九宫格:012012012; 第4个九宫格:345345345; 第5个九宫格:678678678;
第6个九宫格:012012012; 第7个九宫格:345345345; 第8个九宫格:678678678;
可见对于下个九宫格列号增3,循环周期为3;对于单个九宫格,每个格点行号增1,周期也为3。
周期的数学表示就是取模运算mod。
因此第i个九宫格的第j个格点的列号可表示为i%3*3+j%3
故为:1
board[i/3*3 + j/3][i%3*3 + j%3]
具体代码如下: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
30class Solution {
public boolean isValidSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
Map<Character, Boolean> judge_row = new HashMap<Character, Boolean>();
Map<Character, Boolean> judge_col = new HashMap<Character, Boolean>();
Map<Character, Boolean> judge_sub = new HashMap<Character, Boolean>();
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
if (judge_row.containsKey(board[i][j])) {
return false;
}
judge_row.put(board[i][j], true);
}
if (board[j][i] != '.') {
if (judge_col.containsKey(board[j][i])) {
return false;
}
judge_col.put(board[j][i], true);
}
if (board[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3] != '.') {
if (judge_sub.containsKey(board[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3])) {
return false;
}
judge_sub.put(board[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3], true);
}
}
}
return true;
}
}