-
Notifications
You must be signed in to change notification settings - Fork 0
/
P130_SurroundedRegions.py
32 lines (30 loc) · 1.02 KB
/
P130_SurroundedRegions.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
#coding=utf-8
'''
url: leetcode.com/problems/surrounded-regions
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年5月13日
@details: Solution: 146ms 80.52%
'''
class Solution(object):
def solve(self, b):
"""
:type b: List[List[str]]
:rtype: void Do not return anything, modify b in-place instead.
"""
rn = 0 if b == None else len(b)
if rn < 3: return
cn = 0 if b[0] == None else len(b[0])
if cn < 3: return
save = [ij for k in range(rn+cn) for ij in ((0, k), (rn-1, k), (k, 0), (k, cn-1))]
while save:
i, j = save.pop()
if 0 <= i < rn and 0 <= j < cn and b[i][j] == 'O':
b[i][j] = '1'
save += (i, j-1), (i, j+1), (i-1, j), (i+1, j)
for i in range(rn):
for j in range(cn):
if b[i][j] == '1':
b[i][j] = 'O'
elif b[i][j] == 'O':
b[i][j] = 'X'