forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsValidSudoKu.java
More file actions
35 lines (30 loc) · 1.16 KB
/
IsValidSudoKu.java
File metadata and controls
35 lines (30 loc) · 1.16 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
package tree;
import java.util.HashMap;
public class IsValidSudoKu {
public boolean isValidSudoku(char[][] board){
HashMap<Integer,Integer> [] rows = new HashMap[9];
HashMap<Integer,Integer>[] cols = new HashMap[9];
HashMap<Integer,Integer>[] boxes = new HashMap[9];
for (int i = 0; i < 9; i++) {
rows[i] = new HashMap<Integer, Integer>();
cols[i] = new HashMap<Integer, Integer>();
boxes[i] = new HashMap<Integer, Integer>();
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char num = board[i][j];
if (num != '.'){
int n = (int)num;
int box_index = (i/3)*3+j/3;
rows[i].put(n,rows[i].getOrDefault(n,0)+1);
cols[j].put(n,cols[j].getOrDefault(n,0)+1);
boxes[box_index].put(n,boxes[box_index].getOrDefault(n,0)+1);
if (rows[i].get(n) >1 || cols[j].get(n) >1 || boxes[box_index].get(n)>1){
return false;
}
}
}
}
return true;
}
}