You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Given a 2d grid map of
'1'
s (land) and'0'
s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.Example 1:
Example 2:
这道题的做法是DFS和BFS
为什么我一开始想不到:(。DFS建立一个二维访问数组v,遍历原数组,对原数组每个非'0'元素的四个方向作深度优先遍历。BFS同样遍历数组,但不同的是对每个非'0'元素建立一个队列,也是对四个方向遍历,符合条件的加入队列,循环直至队列为空。
DFS代码如下:
bfs代码如下:
参考资料:
The text was updated successfully, but these errors were encountered: