← 返回 amazon 的题目列表Island Counter
类型:online_judge
Given a grid consisting of '1' (land) and '0' (water), calculate the number of islands in the grid. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Assume that all four edges of the grid are surrounded by water.
Input:
A 2D character grid
Output:
The number of islands
Examples
Example 1:
Input:
[["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]]
Output: 3
Example 2:
Input:
[["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]]
Output: 1
Constraints
The input is a 2D grid with dimensions m x n.
1 <= m, n <= 300
Example
Input
[["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]