-
Notifications
You must be signed in to change notification settings - Fork 0
/
200.py
109 lines (86 loc) · 3.38 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
示例 1:
输入:
[
['1','1','1','1','0'],
['1','1','0','1','0'],
['1','1','0','0','0'],
['0','0','0','0','0']
]
输出: 1
示例 2:
输入:
[
['1','1','0','0','0'],
['1','1','0','0','0'],
['0','0','1','0','0'],
['0','0','0','1','1']
]
输出: 3
解释: 每座岛屿只能由水平和/或竖直方向上相邻的陆地连接而成。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-islands
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
"""
from typing import List
class Solution:
# 深度优先
def numIslands_1(self, grid: List[List[str]]) -> int:
if not grid:
return 0
num_rows, num_cols = len(grid), len(grid[0])
checked = [[0] * num_cols for _ in range(num_rows)]
def dfs(row_id, col_id):
if (0 <= row_id < num_rows) and (0 <= col_id < num_cols) and (grid[row_id][col_id] == '1'):
if checked[row_id][col_id] == 1:
return
else:
checked[row_id][col_id] = 1
dfs(row_id + 1, col_id)
dfs(row_id - 1, col_id)
dfs(row_id, col_id - 1)
dfs(row_id, col_id + 1)
num_islands = 0
for row in range(num_rows):
for col in range(num_cols):
if grid[row][col] == '1' and checked[row][col] == 0:
dfs(row, col)
num_islands += 1
return num_islands
# 广度优先
def numIslands(self, grid: List[List[str]]) -> int:
if not grid:
return 0
num_rows, num_cols = len(grid), len(grid[0])
num_islands = 0
for row in range(num_rows):
for col in range(num_cols):
if grid[row][col] == '1':
num_islands += 1
grid[row][col] = '0'
neighbors = [[row, col]]
while len(neighbors) > 0:
neighbor_row, neighbor_col = neighbors.pop(0)
for row_id, col_id in [(neighbor_row - 1, neighbor_col),
(neighbor_row + 1, neighbor_col),
(neighbor_row, neighbor_col - 1),
(neighbor_row, neighbor_col + 1)]:
if (0 <= row_id < num_rows) and (0 <= col_id < num_cols) and (grid[row_id][col_id] == '1'):
grid[row_id][col_id] = '0'
neighbors.append([row_id, col_id])
return num_islands
grid = [['1', '1', '1', '1', '0'],
['1', '1', '0', '1', '0'],
['1', '1', '0', '0', '0'],
['0', '0', '0', '0', '0']]
grid = [['1', '1', '0', '0', '0'],
['1', '1', '0', '0', '0'],
['0', '0', '1', '0', '0'],
['0', '0', '0', '1', '1']]
grid = [["1", "1", "1"],
["1", "0", "1"],
["1", "1", "1"]]
print(Solution().numIslands(grid))