题目描述
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:
1 | Input: |
Example 2:
1 | Input: |
这题需要一个额外的二维数组,来记录某一个点是否被检查过。然后我们需要对所有的点按顺序进行一次深度优先搜索,如果说这个点已经被检查过,则不再搜索。那么,一开始需要进行搜索的点的个数,就是岛屿的数量。
代码实现
1 | class Solution: |