-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
executable file
·47 lines (43 loc) · 1.31 KB
/
Solution.java
File metadata and controls
executable file
·47 lines (43 loc) · 1.31 KB
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
45
46
47
package $036;
import java.util.HashSet;
import java.util.Set;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 19:00 2018/5/16.
*/
public class Solution {
public boolean isValidSudoku(char[][] board) {
HashSet<Character> row = new HashSet<>();
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
if (board[i][j] != '.' && !row.add(board[i][j])){
return false;
}
}
row.clear();
}
HashSet<Character> column = new HashSet<>();
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
if (board[j][i] != '.' && !column.add(board[j][i])){
return false;
}
}
column.clear();
}
HashSet<Character> set = new HashSet<>();
for(int m = 0; m < 3; m ++){
for(int n = 0; n < 3; n ++){
for(int i = m * 3; i < m * 3 + 3; i ++){
for(int j = n * 3; j < n * 3 + 3; j ++){
if(board[i][j] != '.' && !set.add(board[i][j])){
return false;
}
}
}
set.clear();
}
}
return true;
}
}