-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSurrounded Regions (Python 3.5)
More file actions
32 lines (28 loc) · 1013 Bytes
/
Copy pathSurrounded Regions (Python 3.5)
File metadata and controls
32 lines (28 loc) · 1013 Bytes
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
class Solution:
def mark_border(self, i, j, board):
if i==-1 or i==len(board):
return
if j==-1 or j==len(board[0]):
return
if board[i][j]=='O':
board[i][j]=''
self.mark_border(i-1, j, board)
self.mark_border(i+1, j, board)
self.mark_border(i, j-1, board)
self.mark_border(i, j+1, board)
def solve(self, board):
if not board or not board[0]:
return []
M, N = len(board), len(board[0])
for i in range(M):
self.mark_border(i, 0, board)
self.mark_border(i, N-1, board)
for j in range(N):
self.mark_border(0, j, board)
self.mark_border(M-1, j, board)
for i in range(M):
for j in range(N):
if board[i][j]=='':
board[i][j]='O'
elif board[i][j]=='O':
board[i][j]='X'