We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 88cddc9 commit 7b7431aCopy full SHA for 7b7431a
number-of-islands/evan.py
@@ -0,0 +1,29 @@
1
+from typing import List
2
+
3
4
+class Solution:
5
+ def numIslands(self, grid: List[List[str]]) -> int:
6
+ if not grid:
7
+ return 0
8
9
+ rows, cols = len(grid), len(grid[0])
10
+ count = 0
11
12
+ def dfs(r, c):
13
+ if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == "0":
14
+ return
15
16
+ grid[r][c] = "0"
17
18
+ dfs(r + 1, c)
19
+ dfs(r - 1, c)
20
+ dfs(r, c + 1)
21
+ dfs(r, c - 1)
22
23
+ for row in range(rows):
24
+ for col in range(cols):
25
+ if grid[row][col] == "1":
26
+ count += 1
27
+ dfs(row, col)
28
29
+ return count
0 commit comments