-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_NumOfIslands.py
More file actions
36 lines (30 loc) · 1.02 KB
/
Copy pathGraph_NumOfIslands.py
File metadata and controls
36 lines (30 loc) · 1.02 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
from collections import deque
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
if not grid:
return None
rows = len(grid)
cols = len(grid[0])
visited = set()
islands = 0
def bfs(i, j):
q = deque()
visited.add((i, j))
q.append((i, j))
while q:
row, col = q.popleft()
directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]
for r, c in directions:
i, j = row+r, col+c
if (i in range(rows) and
j in range(cols) and
grid[i][j] == "1" and
(i, j) not in visited):
q.append((i, j))
visited.add((i, j))
for i in range(rows):
for j in range(cols):
if grid[i][j] == "1" and (i, j) not in visited:
bfs(i, j)
islands += 1
return islands