-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution2.py
27 lines (25 loc) · 998 Bytes
/
Solution2.py
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
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
m, n = len(board), len(board[0])
p = list(range(m * n + 1))
for i in range(m):
for j in range(n):
if board[i][j] == 'O':
if i == 0 or i == m - 1 or j == 0 or j == n - 1:
p[find(i * n + j)] = find(m * n)
else:
for a, b in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
x, y = i + a, j + b
if board[x][y] == 'O':
p[find(x * n + y)] = find(i * n + j)
for i in range(m):
for j in range(n):
if board[i][j] == 'O' and find(i * n + j) != find(m * n):
board[i][j] = 'X'