-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00200-number_of_islands.py
54 lines (42 loc) · 1.15 KB
/
00200-number_of_islands.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
# 200: Number Of Islands
# https://leetcode.com/problems/number-of-islands/
from typing import List
class Solution:
# SOLUTION
def numIslands(self, grid: List[List[str]]) -> int:
if not grid or not grid[0]:
return 0
result = 0
visit = set()
rows, cols = len(grid), len(grid[0])
def dfs(i, j):
if (
i not in range(rows)
or j not in range(cols)
or grid[i][j] == "0"
or (i, j) in visit
):
return
visit.add((i, j))
dfs(i + 1, j);
dfs(i, j + 1);
dfs(i - 1, j);
dfs(i, j - 1);
for i in range(rows):
for j in range(cols):
if grid[i][j] == "1" and (i, j) not in visit:
result += 1
dfs(i, j)
return result
if __name__ == "__main__":
o = Solution()
# INPUT
grid =[
['1','1','1','1','0'],
['1','1','0','1','0'],
['1','1','0','0','0'],
['0','0','0','0','0']
]
# OUTPUT
result = o.numIslands(grid)
print(result)