forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
surrounded-regions.py
47 lines (39 loc) · 1.38 KB
/
surrounded-regions.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Time: O(m * n)
# Space: O(m + n)
import collections
class Solution(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
if not board:
return
q = collections.deque()
for i in xrange(len(board)):
if board[i][0] == 'O':
board[i][0] = 'V'
q.append((i, 0))
if board[i][len(board[0])-1] == 'O':
board[i][len(board[0])-1] = 'V'
q.append((i, len(board[0])-1))
for j in xrange(1, len(board[0])-1):
if board[0][j] == 'O':
board[0][j] = 'V'
q.append((0, j))
if board[len(board)-1][j] == 'O':
board[len(board)-1][j] = 'V'
q.append((len(board)-1, j))
while q:
i, j = q.popleft()
for x, y in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
if 0 <= x < len(board) and 0 <= y < len(board[0]) and \
board[x][y] == 'O':
board[x][y] = 'V'
q.append((x, y))
for i in xrange(len(board)):
for j in xrange(len(board[0])):
if board[i][j] != 'V':
board[i][j] = 'X'
else:
board[i][j] = 'O'