-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValidSudoku.java
54 lines (52 loc) · 1.95 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
/**
* 最重要的是,如何在i,j的递归中,将相应的board位置表达出来。
* 1. 对于行,直接board[i][j]可以表达。
* 2. 对于列,直接board[j][i]可以表达。
* 3. 对于小方块,先根据i算出行和列的起点,然后根据j算出小的行和列的bias。比较复杂一点,需要理解。
* @author LBW
*/
@SuppressWarnings("ALL")
public class ValidSudoku {
public boolean isValidSudoku(char[][] board) {
// init three arrays, as HashSet.
boolean[] row = new boolean[9];
boolean[] colume = new boolean[9];
boolean[] cube = new boolean[9];
//start to traverse the board.
for (int i = 0; i < 9; i++) {
init(row);
init(colume);
init(cube);
int rowIndex = (i / 3) * 3, columeIndex = (i % 3) * 3; //used for cube judgement.
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
//judge row
if (!row[board[i][j] - '1']) {
row[board[i][j] - '1'] = true;
} else
return false;
}
if (board[j][i] != '.') {
//judge colume
if (!colume[board[j][i] - '1']) {
colume[board[j][i] - '1'] = true;
} else
return false;
}
if (board[rowIndex + j / 3][columeIndex + j % 3] != '.') {
//judge cube
if (!cube[board[rowIndex + j / 3][columeIndex + j % 3] - '1']) {
cube[board[rowIndex + j / 3][columeIndex + j % 3] - '1'] = true;
}
else
return false;
}
}
}
return true;
}
private void init(boolean[] nums) {
for (int i = 0; i < 9; i++)
nums[i] = false;
}
}