File tree 1 file changed +55
-0
lines changed
1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ import java .util .HashSet ;
3
+
4
+ public class Medium36 {
5
+
6
+ public boolean isValidSudoku (char [][] board ) {
7
+ int N = 9 ;
8
+
9
+ // Use hash set to record the status
10
+ HashSet <Character >[] rows = new HashSet [N ];
11
+ HashSet <Character >[] cols = new HashSet [N ];
12
+ HashSet <Character >[] boxes = new HashSet [N ];
13
+ for (int r = 0 ; r < N ; r ++) {
14
+ rows [r ] = new HashSet <Character >();
15
+ cols [r ] = new HashSet <Character >();
16
+ boxes [r ] = new HashSet <Character >();
17
+ }
18
+
19
+ for (int r = 0 ; r < N ; r ++) {
20
+ for (int c = 0 ; c < N ; c ++) {
21
+ char val = board [r ][c ];
22
+
23
+ // Check if the position is filled with number
24
+ if (val == '.' ) {
25
+ continue ;
26
+ }
27
+
28
+ // Check the row
29
+ if (rows [r ].contains (val )) {
30
+ return false ;
31
+ }
32
+ rows [r ].add (val );
33
+
34
+ // Check the column
35
+ if (cols [c ].contains (val )) {
36
+ return false ;
37
+ }
38
+ cols [c ].add (val );
39
+
40
+ // Check the box
41
+ int idx = (r / 3 ) * 3 + c / 3 ;
42
+ if (boxes [idx ].contains (val )) {
43
+ return false ;
44
+ }
45
+ boxes [idx ].add (val );
46
+ }
47
+ }
48
+ return true ;
49
+
50
+
51
+ }
52
+
53
+
54
+
55
+ }
You can’t perform that action at this time.
0 commit comments