题目
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: |
思路
DFS.
遍历时对每个“1”进行dfs,把它及相连的位置全部变成“0”,这样就能把一个点推广到一个岛,从而得到岛的数目。
代码
1 | class Solution(object): |