-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidSudoku.java
86 lines (84 loc) · 2.15 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/********************************************************
> File Name:36 ValidSudoku.java
> Auther: ihochang
> Mail: [email protected]
> Created Time: Fri Jan 22 19:55:20 2016
*********************************************************/
import java.util.ArrayList;
import java.util.Arrays;
public class ValidSudoku {
public boolean isValidSudoku(char[][] board) {
boolean col = checkCol(board);
boolean row = checkRow(board);
boolean sq = checkSq(board);
if (col == true && row == true && sq == true) {
return true;
} else {
return false;
}
}
public boolean checkCol(char[][] board) {
ArrayList<Character> ele = new ArrayList<Character>();
for (int i = 0;i<board.length;i++) {
for (int j = 0;j<board.length;j++){
if (board[j][i] == '.') {
continue;
}
if (ele.indexOf(board[j][i]) != -1) {
return false;
} else {
ele.add(board[j][i]);
}
}
ele.clear();
}
return true;
}
public boolean checkRow(char[][] board) {
ArrayList<Character> ele = new ArrayList<Character>();
for (int i = 0;i<board.length;i++) {
for (int j = 0;j<board.length;j++) {
if (board[i][j] == '.') {
continue;
}
if (ele.indexOf(board[i][j]) != -1) {
return false;
} else {
ele.add(board[i][j]);
}
}
ele.clear();
}
return true;
}
public boolean checkSq(char[][] board) {
ArrayList<Character> ele = new ArrayList<Character>();
for (int i = 0;i<board.length/3;i=i+3) {
for (int j = 0;j<board.length/3;j=j+3) {
for (int m = 0;m<3;m++) {
for (int n = 0;n<3;n++) {
if (board[i+m][j+n] == '.') {
continue;
}
if (ele.indexOf(board[i+m][j+n]) != -1) {
return false;
} else {
ele.add(board[i+m][j+n]);
}
}
}
ele.clear();
}
}
return true;
}
public static void main(String[] args) {
ValidSudoku so = new ValidSudoku();
String[] test = {".87654321","2........","3........","4........","5........","6........","7........","8........","9........"};
char[][] testChar = new char[9][9];
for (int i = 0;i<9;i++) {
testChar [i] = test[i].toCharArray();
}
System.out.println(so.isValidSudoku(testChar));
}
}