-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidSudoku.java
43 lines (40 loc) · 1.4 KB
/
ValidSudoku.java
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
class Solution {
public boolean isValidSudoku(char[][] board) {
HashSet<Integer> rowSet = new HashSet<Integer>();
for(int i = 0; i < 9; i++) {
rowSet.clear();
for(int j = 0; j < 9; j++) {
if(board[i][j] == '.')continue;
int num = board[i][j] - '0';
if(num>9 || num < 1)return false;
if(rowSet.contains(num))return false;
rowSet.add(num);
}
}
for(int i = 0; i < 9; i++) {
rowSet.clear();
for(int j = 0; j < 9; j++) {
if(board[j][i] == '.')continue;
int num = board[j][i] - '0';
if(num>9 || num < 1)return false;
if(rowSet.contains(num))return false;
rowSet.add(num);
}
}
for(int k = 0; k < 3; k++) {
for(int l = 0; l < 3; l++) {
rowSet.clear();
for(int i = 0+3*k; i < 3+3*k; i++) {
for(int j = 0+3*l; j < 3+3*l; j++) {
if(board[j][i] == '.')continue;
int num = board[j][i] - '0';
if(num>9 || num < 1)return false;
if(rowSet.contains(num))return false;
rowSet.add(num);
}
}
}
}
return true;
}
}