-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
34 lines (32 loc) · 1013 Bytes
/
index.ts
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
26
27
28
29
30
31
32
33
34
/*
* @lc app=leetcode.cn id=36 lang=typescript
*
* [36] 有效的数独
*/
// @lc code=start
export default function isValidSudoku(board: string[][]): boolean {
const rows = new Array(9).fill(0).map(() => new Set<string>());
const columns = new Array(9).fill(0).map(() => new Set<string>());
const subboxes = new Array(3)
.fill(0)
.map(() => new Array(3).fill(0).map(() => new Set<string>()));
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
const c = board[i][j];
if (c !== ".") {
if (
rows[i].has(c) ||
columns[j].has(c) ||
subboxes[Math.floor(i / 3)][Math.floor(j / 3)].has(c)
) {
return false;
}
rows[i].add(c);
columns[j].add(c);
subboxes[Math.floor(i / 3)][Math.floor(j / 3)].add(c);
}
}
}
return true;
}
// @lc code=end