Number of Islands
Problem
Given a boolean 2D matrix, find the number of islands.
Example
Given graph:
[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]
return 3
.
Note
0
is represented as the sea, 1
is represented as the island. If two 1
is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent.
Solution
A very basic BFS / DFS problem.