Counting Islands
DEPTH-FIRST SEARCH
MATRIX
BREADTH-FIRST SEARCH
Problem
Given a 2D grid grid
of 1s (land) and 0s (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 surrounded by water.
Examples
numIslands([ [1, 1, 0], [1, 0, 0], [0, 1, 1] ]) // returns 2 /* There are two islands in the grid. The first island is at [(0,0), (0,1), (1,0)]. The second island is at [(2,1), (2,2)]. */
Loading...
1
1
0
1
0
0
0
1
1