Skip to content

Commit 5e80bab

Browse files
authored
Update 36-valid-sudoku.js
1 parent 47b56fe commit 5e80bab

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

36-valid-sudoku.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
/**
2+
* @param {character[][]} board
3+
* @return {boolean}
4+
*/
5+
const isValidSudoku = function(board) {
6+
const n = 9
7+
const m = 3
8+
const row = [],
9+
col = [],
10+
block = []
11+
for (let i = 0; i < n; i++) {
12+
row[i] = new Set()
13+
col[i] = new Set()
14+
block[i] = new Set()
15+
}
16+
for (let r = 0; r < n; r++) {
17+
for (let c = 0; c < n; c++) {
18+
const ch = board[r][c]
19+
if (ch === '.') continue
20+
const b = Math.floor(r / m) * m + Math.floor(c / m)
21+
if (row[r].has(ch) || col[c].has(ch) || block[b].has(ch)) return false
22+
row[r].add(ch)
23+
col[c].add(ch)
24+
block[b].add(ch)
25+
}
26+
}
27+
return true
28+
}
29+
30+
// another
31+
132
/**
233
* @param {character[][]} board
334
* @return {boolean}

0 commit comments

Comments
 (0)