← 返回 linkedin 的题目列表Number of Islands
类型:online_judge
Given a m x n 2D grid map of '1's (land) and '0's (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 all surrounded by water.
Input Format:
A 2D character grid grid where 1 represents land and 0 represents water. The range of m (rows) and n (columns) are [1, 300].
Output Format:
Output an integer representing the number of islands.
Example:
Input: grid = [
['1','1','1','1','0'],
['1','1','0','1','0'],
['1','1','0','0','0'],
['0','0','0','0','0']
]
Output: 1
Example
Input
1 1 1 1 0
1 1 0 1 0
1 1 0 0 0
0 0 0 0 0