-
Notifications
You must be signed in to change notification settings - Fork 2
/
Que36.java
86 lines (62 loc) · 1.82 KB
/
Que36.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
import java.util.Arrays;
/**
* @author: hyl
* @date: 2019/08/07
**/
public class Que36 {
//暴力,时间复杂度为O(N^2)
public boolean isValidSudoku(char[][] board) {
int[] result = new int[10];
//判断行是否合法
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] != '.'){
result[board[i][j] - '0']++;
}
}
for (int k = 0; k < result.length; k++) {
if (result[k] > 1){
return false;
}
}
Arrays.fill(result,0);
}
//判断列是否合法
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[j][i] != '.'){
result[board[j][i] - '0']++;
}
}
for (int k = 0; k < result.length; k++) {
if (result[k] > 1){
return false;
}
}
Arrays.fill(result,0);
}
//判断每个3x3的宫格内是否合法
int row = 0 , col = 0;
while (row < 9){
for (int i = row; i <row+3 ; i++) {
for (int j = col; j < col+3; j++) {
if (board[i][j] != '.'){
result[board[i][j] - '0']++;
}
}
}
for (int i = 0; i < result.length; i++) {
if (result[i] > 1){
return false;
}
}
Arrays.fill(result,0);
col += 3;
if (col >= 9){
row += 3;
col = 0;
}
}
return true;
}
}