-
Notifications
You must be signed in to change notification settings - Fork 0
/
200.py
57 lines (47 loc) · 1.72 KB
/
200.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
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
m = len(grid)
if m == 0:
return 0
n = len(grid[0])
line1 = [0] * n
total = 0
new_flag = 1
for i in xrange(0, m):
line2 = []
for j in xrange(0, n):
if grid[i][j] == '0':
line2.append(0)
else:
if (grid[i-1][j] == '0' or i-1 < 0) and (grid[i][j-1] == '0' or j-1 < 0):
line2.append(new_flag)
new_flag += 1
total += 1
elif i-1 >= 0 and grid[i-1][j] == '1' and j-1 >= 0 and grid[i][j-1] == '1':
if line1[j] == line2[j-1]:
pass
else:
t = line1[j]
for k in xrange(0, n):
if k <= j-1 and line2[k] == t:
line2[k] = line2[j-1]
elif k >= j and line1[k] == t:
line1[k] = line2[j-1]
total -= 1
line2.append(line2[j-1])
else:
if j-1 >= 0 and grid[i][j-1] == '1':
line2.append(line2[j-1])
else:
line2.append(line1[j])
line1 = line2
return total
if __name__ == "__main__":
print Solution().numIslands(["1", "1"])