-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200.py
46 lines (42 loc) · 1.28 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
from typing import List
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
if not grid:
return 0
m, n = len(grid), len(grid[0])
marked = set()
num = 0
for i in range(m):
for j in range(n):
if (i, j) in marked:
continue
if grid[i][j] == "0":
marked.add((i, j))
continue
num += 1
labors = [(i, j)]
while labors:
r, c = labors.pop()
if 0 <= r < m and 0 <= c < n:
if (r, c) in marked:
continue
marked.add((r, c))
if grid[r][c] == "1":
labors.extend([(r, c - 1), (r, c + 1), (r - 1, c), (r + 1, c)])
return num
if __name__ == '__main__':
s = Solution()
print(s.numIslands([
['1', '1', '1', '1', '0'],
['1', '1', '0', '1', '0'],
['1', '1', '0', '0', '0'],
['0', '0', '0', '0', '0']
]
))
print(s.numIslands([
['1', '1', '0', '0', '0'],
['1', '1', '0', '0', '0'],
['0', '0', '1', '0', '0'],
['0', '0', '0', '1', '1']
]
))