Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
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:
自己只想到了采用暴力的解法,即通过三个循环,依次分别检查行、列和每个九宫格是否有效。并借助 hashmap 进行记录便于判断。
class Solution {
public boolean isValidSudoku(char[][] board) {
HashMap<Character, Integer> map = new HashMap<>();
//判断每行
for(int i=0; i<9; i++) {
map.clear();
for(int j=0; j<9; j++) {
if(board[i][j] != '.' && map.containsKey(board[i][j])) {
return false;
} else {
map.put(board[i][j], 1);
}
}
}
//判断每列
for(int j=0; j<9; j++) {
map.clear();
for(int i=0; i<9; i++) {
if(board[i][j] != '.' && map.containsKey(board[i][j])) {
return false;
} else {
map.put(board[i][j], 1);
}
}
}
//判断每个九宫格
int[][] flag = {{0,0}, {0,3}, {0,6}, {3,0}, {3,3}, {3,6}, {6,0}, {6,3}, {6,6} };
for(int k=0; k<9; k++) {
map.clear();
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
int row = flag[k][0] + i;
int column = flag[k][1] + j;
if(board[row][column] != '.' && map.containsKey(board[row][column])) {
return false;
} else {
map.put(board[row][column], 1);
}
}
}
}
return true;
}
}
后来看了他人的思路,对解法有一定的优化
这道题的三种情况判断中,其中最复杂的是第三种对九个九宫格的判断。我们对每个九宫格中的行列进行找规律,将其与行列序号关联起来。
9*9 数独可分为九个九宫格,我们从上至下,从左至右依次将九个九宫格标号为0~9
观察行号可得:
0️⃣ 0 0 0 ① 0 0 0 ② 0 0 0
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
③ 3 3 3 ④ 3 3 3 ⑤ 3 3 3
4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
⑥ 6 6 6 ⑦ 6 6 6 ⑧ 6 6 6
7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8
对于每三个九宫格行号增3。对于单个九宫格,每三个格点行号增1。
因此第i个九宫格的第j个格点的行号可表示为i/3*3+j/3(每个小九宫格j都是从0~9递增)
观察列号可得:
0️⃣ 0 1 2 ① 3 4 5 ② 6 7 8
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
③ 0 1 2 ④ 3 4 5 ⑤ 6 7 8
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
⑥ 0 1 2 ⑦ 3 4 5 ⑧ 6 7 8
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
对于下个九宫格列号增3,循环周期为3。对于单个九宫格,每个格点列号增1,周期也为3。
因此第i个九宫格的第j个格点的列号可表示为i%3*3+j%3(每个小九宫格j都是从0~9递增)
因此,结合上述规律和 hashmap,我们可得到一下优化后的解法:
class Solution {
public boolean isValidSudoku(char[][] board) {
for(int i=0; i<9; i++) {
HashSet<Character> row = new HashSet<>();
HashSet<Character> column = new HashSet<>();
HashSet<Character> sub = new HashSet<>();
for(int j=0; j<9; j++) {
//检查第i行
if(board[i][j] !='.' && !row.add(board[i][j]))
return false;
//检查第i列
if(board[j][i] !='.' && !column.add(board[j][i]))
return false;
//检查第i个九宫格
int rowIndex = i/3*3 + j/3;
int columnIndex = i%3*3 + j%3;
if(board[rowIndex][columnIndex] !='.' && !sub.add(board[rowIndex][columnIndex]))
return false;
}
}
return true;
}
}
注意:HashSet 的 add() 方法
HashSet不能添加重复的元素,当调用 add(Object) 方法时,首先会调用 Object 的 hashCode 方法判 hashCode 是否已经存在,如不存在则直接插入元素。如果已存在则调用 Object 对象的 equals 方法判断返回值
add() 方法有返回值,如果传入的 Set 中存在的重复值,则表示 add 失败并返回 fasle,传入不存在的新值表示add成功,并返回true