← 返回 linkedin 的题目列表Find Island Variant
类型:online_judge
Given a grid map where '1' represents land and '0' represents water. Write an algorithm to count the number of islands in the grid. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Assume all four edges of the grid are surrounded by water. Input: A 2D grid consisting of '1's (land) and '0's (water). Output: An integer representing the number of islands. Constraints: The grid size does not exceed 300x300. python def count_islands(grid: List[List[str]]) -> int: pass
Example
Input
[['1', '1', '0', '0', '0'], ['1', '1', '0', '0', '0'], ['0', '0', '1', '0', '0'], ['0', '0', '0', '1', '1']]