← 返回 microsoft 的题目列表Number of Islands
类型:online_judge
Problem Description
Given an m x n grid of '1's (land) and '0's (water), return 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
A 2D grid map of '1's (land) and '0's (water).
Output
An integer representing the number of islands.
Test Cases
Input: [ ['1', '1', '0', '0', '0'], ['1', '1', '0', '0', '0'], ['0', '0', '1', '0', '0'], ['0', '0', '0', '1', '1'] ] Output: 3
Input: [ ['1', '0', '0'], ['0', '1', '1'], ['0', '0', '0'] ] Output: 2
Input: [ ['1', '1', '1'], ['1', '0', '1'], ['1', '1', '1'] ] Output: 1
Constraints
The grid may contain at most 1000 cells.
Example
Input
111001",