← 返回 microsoft 的题目列表Number of Islands
类型:online_judge
Number of Islands
Given a grid composed of '1' (land) and '0' (water), compute the number of islands in the grid. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume that all four edges of the grid are surrounded by water.
Input:
A 2D character grid where grid[i][j] = '1' represents land, and grid[i][j] = '0' represents water.
Output:
The number of islands in the grid.
Example:
Input:
[
['1','1','0','0','0'],
['1','1','0','0','0'],
['0','0','1','0','0'],
['0','0','0','1','1']
]
Output: 3
Constraints:
The length and width of the grid do not exceed 300.
Example
Input
[['1', '1', '0', '0', '0'], ['1', '1', '0', '0', '0'], ['0', '0', '1', '0', '0'], ['0', '0', '0', '1', '1']]