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

[pull] master from youngyangyang04:master #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 3 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions problems/0200.岛屿数量.广搜版.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,51 @@ class Solution {
}
}
```

## 其他语言版本
### Python
BFS solution
```python
class Solution:
def __init__(self):
self.dirs = [[0, 1], [1, 0], [-1, 0], [0, -1]]

def numIslands(self, grid: List[List[str]]) -> int:
m = len(grid)
n = len(grid[0])
visited = [[False]*n for _ in range(m)]
res = 0
for i in range(m):
for j in range(n):
if visited[i][j] == False and grid[i][j] == '1':
res += 1
self.bfs(grid, i, j, visited) # Call bfs within this condition
return res

def bfs(self, grid, i, j, visited):
q = deque()
q.append((i,j))
visited[i][j] = True
while q:
x, y = q.popleft()
for k in range(4):
next_i = x + self.dirs[k][0]
next_j = y + self.dirs[k][1]

if next_i < 0 or next_i >= len(grid):
continue
if next_j < 0 or next_j >= len(grid[0]):
continue
if visited[next_i][next_j]:
continue
if grid[next_i][next_j] == '0':
continue
q.append((next_i, next_j))
visited[next_i][next_j] = True
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
```

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