forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.js
25 lines (25 loc) · 804 Bytes
/
Solution.js
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
/**
* @param {character[][]} board
* @return {boolean}
*/
var isValidSudoku = function (board) {
let row = [...Array(9)].map(() => Array(9).fill(false));
let col = [...Array(9)].map(() => Array(9).fill(false));
let sub = [...Array(9)].map(() => Array(9).fill(false));
for (let i = 0; i < 9; ++i) {
for (let j = 0; j < 9; ++j) {
const num = board[i][j].charCodeAt() - '1'.charCodeAt();
if (num < 0 || num > 9) {
continue;
}
const k = Math.floor(i / 3) * 3 + Math.floor(j / 3);
if (row[i][num] || col[j][num] || sub[k][num]) {
return false;
}
row[i][num] = true;
col[j][num] = true;
sub[k][num] = true;
}
}
return true;
};