Number of island

Medium; BFS;

2. 描述

给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。 岛屿: 相邻陆地可以组成一个岛屿(相邻:上下左右) 判断岛屿个数。

示例1

输入:

复制返回值:

复制

备注:

3. 描述

  1. BFS

  2. iterate every element in array

  3. at current element, use BFS to search all adjacent element with value =1 and mark them as -1, visited, and count the area of this island

  4. then island count += 1 if the area of current node >0

  5. Time: O(n), Space: O(# of node in this level)

4. Coding

Last updated

Was this helpful?