-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0036ValidSudoku.java
More file actions
117 lines (104 loc) · 3.77 KB
/
_0036ValidSudoku.java
File metadata and controls
117 lines (104 loc) · 3.77 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.heatwave.leetcode.problems;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _0036ValidSudoku {
public static boolean isValidSudoku(char[][] board) {
Map<Integer, List<Integer>> rowMap = new HashMap<>();
for (int i = 0; i < 9; i++) {
List<Integer> list = new ArrayList<>();
for (int j = 0; j < 9; j++) {
list.add(0);
}
rowMap.put(i, list);
}
Map<Integer, List<Integer>> colMap = new HashMap<>();
for (int i = 0; i < 9; i++) {
List<Integer> list = new ArrayList<>();
for (int j = 0; j < 9; j++) {
list.add(0);
}
colMap.put(i, list);
}
Map<Integer, List<Integer>> cubeMap = new HashMap<>();
for (int i = 0; i < 9; i++) {
List<Integer> list = new ArrayList<>();
for (int j = 0; j < 9; j++) {
list.add(0);
}
cubeMap.put(i, list);
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
continue;
}
int num = Integer.parseInt(String.valueOf(board[i][j]));
Integer count = rowMap.get(i).get(num - 1);
rowMap.get(i).set(num - 1, count + 1);
count = colMap.get(j).get(num - 1);
colMap.get(j).set(num - 1, count + 1);
int cubeSeq;
if (j < 3) {
if (i < 3) {
cubeSeq = 0;
} else if (i < 6) {
cubeSeq = 1;
} else {
cubeSeq = 2;
}
} else if (j < 6) {
if (i < 3) {
cubeSeq = 3;
} else if (i < 6) {
cubeSeq = 4;
} else {
cubeSeq = 5;
}
} else {
if (i < 3) {
cubeSeq = 6;
} else if (i < 6) {
cubeSeq = 7;
} else {
cubeSeq = 8;
}
}
count = cubeMap.get(cubeSeq).get(num - 1);
cubeMap.get(cubeSeq).set(num - 1, count + 1);
}
}
boolean res = true;
for (List<Integer> list : rowMap.values()) {
for (Integer count : list) {
if (count > 1) {
res = false;
break;
}
}
}
for (List<Integer> list : colMap.values()) {
for (Integer count : list) {
if (count > 1) {
res = false;
break;
}
}
}
for (List<Integer> list : cubeMap.values()) {
for (Integer count : list) {
if (count > 1) {
res = false;
break;
}
}
}
return res;
}
public static void main(String[] args) {
char[][] board = {{'5', '3', '.', '.', '7', '.', '.', '.', '.'}, {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, {'8', '.', '.', '.', '6', '.', '.', '.', '3'}, {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, {'.', '6', '.', '.', '.', '.', '2', '8', '.'}, {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, {'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
boolean res = isValidSudoku(board);
System.out.println(res);
}
}