-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_sudoku.java
More file actions
65 lines (61 loc) · 2.32 KB
/
valid_sudoku.java
File metadata and controls
65 lines (61 loc) · 2.32 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class Solution {
public boolean isValidSudoku(char[][] board) {
// Character.getNumericValue(
int row = board.length;
int column = board[0].length;
ArrayList<Integer> numberarray = new ArrayList<Integer>();
for(int i = 0; i< 9 ; i++){
numberarray.add(0);
}
for(int i = 0; i < row ; i++){
for(int j = 0; j < column ; j++){
if(board[i][j] == '.')
continue;
int current_num = Character.getNumericValue(board[i][j]);
if(numberarray.get(current_num - 1) > 0){ //get - index
return false;
}else{
numberarray.set(current_num - 1, numberarray.get(current_num - 1) + 1);
}
}
for(int k = 0; k< 9 ; k++){
numberarray.set(k, 0);
}
}
for(int j = 0; j < column ; j++){
for(int i = 0; i < row ; i++){
if(board[i][j] == '.')
continue;
int current_num = Character.getNumericValue(board[i][j]);
if(numberarray.get(current_num - 1) > 0){ //get - index
return false;
}else{
numberarray.set(current_num - 1, numberarray.get(current_num - 1) + 1);
}
}
for(int k = 0; k< 9 ; k++){
numberarray.set(k, 0);
}
}
for(int m = 0; m < 3 ; m++){
for(int n = 0; n < 3 ; n++){
for(int i = m*3; i < m*3+3 ; i++){
for(int j = n*3; j < n*3+3 ; j++){
if(board[i][j] == '.')
continue;
int current_num = Character.getNumericValue(board[i][j]);
if(numberarray.get(current_num - 1) > 0){ //get - index
return false;
}else{
numberarray.set(current_num - 1, numberarray.get(current_num - 1) + 1);
}
}
}
for(int k = 0; k < 9 ; k++){
numberarray.set(k, 0);
}
}
}
return true;
}
}