-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
52 lines (39 loc) · 1.18 KB
/
solution.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
type Char = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type Board = (Char | '.')[][];
// * ----------------------------------------------------------------
/*
* @lc app=leetcode id=36 lang=javascript
*
* [36] Valid Sudoku
*/
// @lc code=start
/**
* @param {character[][]} board
* @return {boolean}
*/
const isValidSudoku = (board: Board) => {
// * ['68 ms', '93.89 %', '37.6 MB', '93.33 %']
type Pools = Set<Char>[];
const rowPools: Pools = Array.from({ length: 9 }, () => new Set<Char>());
const columnPools: Pools = Array.from({ length: 9 }, () => new Set<Char>());
const blockPools: Pools = Array.from({ length: 9 }, () => new Set<Char>());
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
const c = board[row][col];
if (c === '.') continue;
let pool: Set<Char>;
pool = rowPools[row];
if (pool.has(c)) return false;
pool.add(c);
pool = columnPools[col];
if (pool.has(c)) return false;
pool.add(c);
pool = blockPools[~~(row / 3) * 3 + ~~(col / 3)];
if (pool.has(c)) return false;
pool.add(c);
}
}
return true;
};
// @lc code=end
export { isValidSudoku };