判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
数字 1-9
在每一行只能出现一次。
数字 1-9
在每一列只能出现一次。
数字 1-9
在每一个以粗实线分隔的 3x3 宫内只能出现一次。
上图是一个部分填充的有效的数独。
数独部分空格内已填入了数字,空白格用 '.' 表示。
示例 1:
输入:
[
["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"]
]
输出: true
示例 2:
输入:
[
["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"]
]
输出: false
解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
说明:
- 一个有效的数独(部分已被填充)不一定是可解的。
- 只需要根据以上规则,验证已经填入的数字是否有效即可。
- 给定数独序列只包含数字 1-9 和字符 '.' 。
- 给定数独永远是 9x9 形式的。
分析:
题意很容易理解,是一道关于二维数组的题目。只需要满足行、列、九宫格都没有重复的数字就可以返回true,只要有一个地方不满足,返回false即可。
行和列的判断很简单,很容易就可以搞定。
比较难的是九宫格的判断,当然如果用穷举的方法也可以实现,但是让代码写的优雅也是我们的目标。
接下来给出java解答:
解法一:
使用HashSet的不能重复添加相同元素的特性来实现,首先是行和列的判断,接下来针对九宫格创建了9个HashSet,分别盛放9个九宫格的元素,这样的方法很容易理解,但不够优雅,请看解法二。
public boolean isValidSudoku(char[][] board) {
Set<Character> rowSet = new HashSet<>();
Set<Character> lineSet = new HashSet<>();
Set<Character> set1 = new HashSet<>();
Set<Character> set2 = new HashSet<>();
Set<Character> set3 = new HashSet<>();
Set<Character> set4 = new HashSet<>();
Set<Character> set5 = new HashSet<>();
Set<Character> set6 = new HashSet<>();
Set<Character> set7 = new HashSet<>();
Set<Character> set8 = new HashSet<>();
Set<Character> set9 = new HashSet<>();
for (int i = 0; i < 9; i++) {
rowSet.clear();
lineSet.clear();
for (int j = 0; j < 9; j++) {
if (board[j][i] != '.') {
if (!lineSet.add(board[j][i])) {
return false;
}
}
if (board[i][j] == '.') {
continue;
}
if (!rowSet.add(board[i][j])) {
return false;
}
if (j < 3) {
if (i < 3) {
if (!set1.add(board[i][j])) {
return false;
}
}
if (i >= 3 && i < 6) {
if (!set4.add(board[i][j])) {
return false;
}
}
if (i >= 6) {
if (!set7.add(board[i][j])) {
return false;
}
}
}
if (j >= 3 && j < 6) {
if (i < 3) {
if (!set2.add(board[i][j])) {
return false;
}
}
if (i >= 3 && i < 6) {
if (!set5.add(board[i][j])) {
return false;
}
}
if (i >= 6) {
if (!set8.add(board[i][j])) {
return false;
}
}
}
if (j >= 6) {
if (i < 3) {
if (!set3.add(board[i][j])) {
return false;
}
}
if (i >= 3 && i < 6) {
if (!set6.add(board[i][j])) {
return false;
}
}
if (i >= 6) {
if (!set9.add(board[i][j])) {
return false;
}
}
}
}
}
return true;
}
解法二:
这种方法的基本思路与上一种解法相同,不同的是,不需要创建9个Set对象来盛放九宫格的元素,只需要一个就够了。代码中的x
和y
是依靠一定的计算方法将当前遍历的i
和j
转换成九宫格的二维数组坐标。如此就显得非常优雅。
public boolean isValidSudoku(char[][] board) {
Set<Character> rowSet = new HashSet<>();
Set<Character> lineSet = new HashSet<>();
Set<Character> sudokuSet = new HashSet<>();
int k = 0;
int x, y;
for (int i = 0; i < 9; i++) {
rowSet.clear();
lineSet.clear();
sudokuSet.clear();
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.' && !rowSet.add(board[i][j])) {
return false;
}
if (board[j][i] != '.' && !lineSet.add(board[j][i])) {
return false;
}
x = (i / 3) * 3 + (k % 9) / 3;
y = j % 3 + ((k / 9) % 3) * 3;
if (board[x][y] != '.' && !sudokuSet.add(board[x][y])) {
return false;
}
k += 1;
}
}
return true;
}