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