-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathValidSudoku.java
38 lines (35 loc) · 1.29 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
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Created by cpacm on 2017/5/22.
*/
public class ValidSudoku {
public static void main(String[] args) {
System.out.println(isValidSudoku(new char[][]{
{'1', '2', '3', '4', '.', '.', '.', '.', '.'},
{'1', '2', '3', '4', '.', '.', '.', '.', '.'},
{'1', '2', '3', '4', '.', '.', '.', '.', '.'},
{'1', '2', '3', '4', '.', '.', '.', '.', '.'},
{'1', '2', '3', '4', '.', '.', '.', '.', '.'},
{'1', '2', '3', '4', '.', '.', '.', '.', '.'},
{'1', '2', '3', '4', '.', '.', '.', '.', '.'},
{'1', '2', '3', '4', '.', '.', '.', '.', '.'},
{'1', '2', '3', '4', '.', '.', '.', '.', '.'}
}));
}
public static boolean isValidSudoku(char[][] board) {
Set seen = new HashSet();
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
if (board[i][j] != '.') {
String b = "(" + board[i][j] + ")";
if (!seen.add(b + i) || !seen.add(j + b) || !seen.add(i / 3 + b + j / 3))
return false;
}
}
}
return true;
}
}