Number of island
Medium; BFS;
Last updated
Medium; BFS;
Last updated
给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。 岛屿: 相邻陆地可以组成一个岛屿(相邻:上下左右) 判断岛屿个数。
输入:
复制返回值:
复制
BFS
iterate every element in array
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
then island count += 1 if the area of current node >0
Time: O(n), Space: O(# of node in this level)