forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidSudoku.java
57 lines (46 loc) · 1.35 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Arrays;
public class ValidSudoku {
public boolean isValidSudoku(char[][] board) {
int row = board.length, col = board[0].length;
boolean[] f = new boolean[9];
for (int i = 0; i < row; i++) {
Arrays.fill(f, false);
for (int j = 0; j < col; j++) {
if (!check(board[i][j], f)) {
return false;
}
}
}
for (int i = 0; i < col; i++) {
Arrays.fill(f, false);
for (int j = 0; j < row; j++) {
if (!check(board[j][i], f)) {
return false;
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Arrays.fill(f, false);
for (int m = 0; m < 3; m++) {
for (int n = 0; n < 3; n++) {
if (!check(board[i * 3 + m][j * 3 + n], f)) {
return false;
}
}
}
}
}
return true;
}
private boolean check(char c, boolean[] f) {
if (c == '.') {
return true;
} else if (f[c - '1']) {
return false;
} else {
f[c - '0'] = true;
return true;
}
}
}