-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_of_islands.py
More file actions
44 lines (35 loc) · 1.2 KB
/
number_of_islands.py
File metadata and controls
44 lines (35 loc) · 1.2 KB
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
# Given a 2d grid map of '1's (land) and '0's (water),
# count the number of islands.
# An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
# You may assume all four edges of the grid are all surrounded by water.
"""
>>> grid = [['1', '1', '1', '1', '0'], ['1', '1', '0', '1', '0'], ['1', '1', '0', '0', '0'], ['0', '0', '0', '0', '0']]
>>> num_islands(grid)
1
>>> grid = [['1', '1', '0', '0', '0'], ['1', '1', '0', '0', '0'], ['0', '0', '1', '0', '0'], ['0', '0', '0', '1', '1']]
>>> num_islands(grid)
3
>>> grid = [['0', '0', '0', '0', '0']]
>>> num_islands(grid)
0
>>> grid = []
>>> num_islands(grid)
0
"""
from typing import List
def num_islands(grid: List[List[str]]) -> int:
num_islands = 0
if not grid:
return num_islands
lin_len = len(grid)
col_len = len(grid[0])
def dfs(i, j):
if 0 <= i < lin_len and 0 <= j < col_len and grid[i][j] == '1':
grid[i][j] = '0'
list(map(dfs, (i - 1, i + 1, i, i), (j, j, j - 1, j + 1)))
for i in range(lin_len):
for j in range(col_len):
if grid[i][j] == '1':
dfs(i, j)
num_islands += 1
return num_islands