岛屿数量
Number of Islands
题目详情
问题:岛屿数量
考察:数组、深度优先搜索、广度优先搜索
来源:DSA Prep / Citadel
链接:https://www.dsaprep.dev/blog/number-of-islands-leetcode-solution
英文原题
Problem: Number of Islands
Patterns: Array, Depth-First Search, Breadth-First Search
Recency: 6mo
Link: https://www.dsaprep.dev/blog/number-of-islands-leetcode-solution
Source: https://www.dsaprep.dev/blog/citadel-coding-interview-questions/
解析
思路:遍历网格,遇到未访问的陆地就计数并用 DFS/BFS 把与它四联通的陆地全部标记访问。
复杂度:时间 O(mn),空间 O(mn) 最坏递归/队列。
英文解析
Approach: Scan the grid. Whenever an unvisited land cell is found, start DFS/BFS to mark its whole connected component, then increment the island count.
Complexity: Time , space worst case.