File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {character[][] } board
3
+ * @return {boolean }
4
+ */
5
+ var isValidSudoku = function ( board ) {
6
+ const rows = { } ;
7
+ const cols = { } ;
8
+ const squares = { } ;
9
+
10
+ for ( let r = 0 ; r < 9 ; r ++ ) {
11
+ for ( let c = 0 ; c < 9 ; c ++ ) {
12
+
13
+ if ( board [ r ] [ c ] == '.' ) continue ;
14
+
15
+ const grid = ( ( r / 3 ) >> 0 ) + '' + ( ( c / 3 ) >> 0 ) ;
16
+
17
+ if ( ! cols [ c ] ) cols [ c ] = new Set ( ) ;
18
+ if ( ! rows [ r ] ) rows [ r ] = new Set ( ) ;
19
+ if ( ! squares [ grid ] ) squares [ grid ] = new Set ( ) ;
20
+
21
+ if ( rows [ r ] . has ( board [ r ] [ c ] ) || cols [ c ] . has ( board [ r ] [ c ] ) || squares [ grid ] . has ( board [ r ] [ c ] ) ) {
22
+ return false ;
23
+ }
24
+
25
+ cols [ c ] . add ( board [ r ] [ c ] )
26
+ rows [ r ] . add ( board [ r ] [ c ] )
27
+ squares [ grid ] . add ( board [ r ] [ c ] )
28
+ }
29
+ }
30
+
31
+ return true ;
32
+
33
+ } ;
34
+
You can’t perform that action at this time.
0 commit comments