Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit be11452

Browse files
添加python版本三
1 parent 2d57993 commit be11452

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎problems/0200.岛屿数量.深搜版.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,40 @@ class Solution:
278278

279279
return result
280280
```
281+
```python
282+
# 我们用三个状态去标记每一个格子
283+
# 0 代表海水
284+
# 1 代表陆地
285+
# 2 代表已经访问的陆地
286+
class Solution:
287+
def traversal(self, grid, i, j):
288+
m = len(grid)
289+
n = len(grid[0])
290+
291+
if i < 0 or j < 0 or i >= m or j >= n:
292+
return # 越界了
293+
elif grid[i][j] == "2" or grid[i][j] == "0":
294+
return
295+
296+
grid[i][j] = "2"
297+
self.traversal(grid, i - 1, j) # 往上走
298+
self.traversal(grid, i + 1, j) # 往下走
299+
self.traversal(grid, i, j - 1) # 往左走
300+
self.traversal(grid, i, j + 1) # 往右走
301+
302+
def numIslands(self, grid: List[List[str]]) -> int:
303+
res = 0
304+
305+
306+
for i in range(len(grid)):
307+
for j in range(len(grid[0])):
308+
if grid[i][j] == "1":
309+
res += 1
310+
self.traversal(grid, i, j)
311+
312+
return res
313+
```
314+
281315
### JavaScript
282316

283317
```javascript

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /