200. Number of Islands
problem description
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:
algorithm thought
这题不是很难,直接遍历所有,对每个为1的点,进行DFS遍历。没遍历到一个1的节点就赋值为0. 每次进行DFS的时候,将res+1.
code
algorithm analysis
对于二维数组,每个节点最多访问两次,所以总的最多访问的次数是2*n².最后时间复杂度O(n²)
Last updated