-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200.岛屿数量.py
37 lines (34 loc) · 1005 Bytes
/
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
#
# @lc app=leetcode.cn id=200 lang=python3
#
# [200] 岛屿数量
#
# @lc code=start
class Solution:
def numIslands(self, grid) -> int:
if not grid or not grid[0]:
return 0
m, n, res = len(grid), len(grid[0]), 0
def find_island(i, j):
bfs = []
bfs.append([i, j])
while len(bfs) > 0:
i, j = bfs.pop()
if grid[i][j] == '1':
grid[i][j] = '0'
if i - 1 >= 0:
bfs.append([i-1, j])
if j - 1 >= 0:
bfs.append([i, j-1])
if i + 1 < m:
bfs.append([i+1, j])
if j + 1 < n:
bfs.append([i, j+1])
return
for i in range(m):
for j in range(n):
if grid[i][j] == '1':
find_island(i, j)
res += 1
return res
# @lc code=end