forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.ts
37 lines (37 loc) · 1.15 KB
/
Solution2.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
function solve(board: string[][]): void {
const m = board.length;
const n = board[0].length;
const p: number[] = Array(m * n + 1)
.fill(0)
.map((_, i) => i);
const dirs: number[] = [-1, 0, 1, 0, -1];
const find = (x: number): number => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (board[i][j] === 'O') {
if (i === 0 || i === m - 1 || j === 0 || j === n - 1) {
p[find(i * n + j)] = find(m * n);
} else {
for (let k = 0; k < 4; ++k) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (board[x][y] === 'O') {
p[find(x * n + y)] = find(i * n + j);
}
}
}
}
}
}
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (board[i][j] === 'O' && find(i * n + j) !== find(m * n)) {
board[i][j] = 'X';
}
}
}
}