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 e97b0e9 commit c3d4fd8Copy full SHA for c3d4fd8
number-of-islands/printjin-gmailcom.py
@@ -0,0 +1,24 @@
1
+class Solution:
2
+ def numIslands(self, grid):
3
+ if not grid:
4
+ return 0
5
+
6
+ rows, cols = len(grid), len(grid[0])
7
+ count = 0
8
9
+ def dfs(r, c):
10
+ if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == "0":
11
+ return
12
+ grid[r][c] = "0"
13
+ dfs(r+1, c)
14
+ dfs(r-1, c)
15
+ dfs(r, c+1)
16
+ dfs(r, c-1)
17
18
+ for r in range(rows):
19
+ for c in range(cols):
20
+ if grid[r][c] == "1":
21
+ dfs(r, c)
22
+ count += 1
23
24
+ return count
0 commit comments