当前位置: 首页 > news >正文

LeetCode //C - 200. Number of Islands

200. Number of Islands

Given an m x n 2D binary grid grid which represents a map 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.
 

Example 1:

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 2:

Input: grid = [
[“1”,“1”,“0”,“0”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“1”,“0”,“0”],
[“0”,“0”,“0”,“1”,“1”]
]
Output: 3

Constraints:
  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • grid[i][j] is ‘0’ or ‘1’.

From: LeetCode
Link: 200. Number of Islands


Solution:

Ideas:

1. Initialize: The numIslands function initializes the count of islands to zero. It also determines the dimensions of the grid, m (number of rows) and n (number of columns).

2. Iterate through the Grid: The code then iterates over each cell of the grid. If the current cell is ‘1’, it indicates a part of an island that hasn’t been explored yet.

3. DFS to Explore the Island: When a ‘1’ is found, we use the dfs function to explore the entire island. The idea is that once we find a ‘1’, we want to explore all its neighboring cells (up, down, left, and right) to see if they are also part of the same island. If they are, we continue the exploration recursively.

  • In the dfs function, if the current cell is out-of-bounds, or if it is water (i.e., ‘0’), we return immediately without further exploration.
  • If the current cell is ‘1’, we mark it as visited by changing its value to ‘0’. This ensures that we don’t count the same part of an island more than once.
  • We then recursively call dfs on the neighboring cells to continue exploring the island.

4. Counting the Islands: Each time we initiate a DFS exploration from the numIslands function (i.e., every time we find an unexplored ‘1’), we increment our island count by 1. By the end of the iteration over the grid, we would have explored and counted all distinct islands.

5. Return the Count: Finally, numIslands returns the count of islands.

Code:
void dfs(char** grid, int i, int j, int m, int n) {if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') {return;}grid[i][j] = '0';dfs(grid, i - 1, j, m, n); // updfs(grid, i + 1, j, m, n); // downdfs(grid, i, j - 1, m, n); // leftdfs(grid, i, j + 1, m, n); // right
}int numIslands(char** grid, int gridSize, int* gridColSize) {if (grid == NULL || gridSize == 0 || gridColSize == NULL) {return 0;}int m = gridSize;int n = gridColSize[0];int islandCount = 0;for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (grid[i][j] == '1') {islandCount++;dfs(grid, i, j, m, n);}}}return islandCount;
}
http://www.lryc.cn/news/171630.html

相关文章:

  • 使用Python构建强大的网络爬虫
  • 图像处理之《基于语义对象轮廓自动生成的生成隐写术》论文精读
  • Java 字节流
  • 华硕电脑怎么录屏?分享实用录制经验!
  • python学习--python的异常处理机制
  • nacos+Dubbo整合快速入门
  • QT实现钟表
  • 准备我们心爱的IDEA写Jsp
  • 将近 5 万字讲解 Python Django 框架详细知识点(更新中)
  • Arcgis提取每个像元的多波段反射率值
  • JavaScript面试题整理(一)
  • 数据结构:树和二叉树之-堆排列 (万字详解)
  • 爬虫入门基础:深入解析HTTP协议的工作过程
  • k8备份与恢复-Velero
  • 基于Python开发的火车票分析助手(源码+可执行程序+程序配置说明书+程序使用说明书)
  • 旺店通·企业奇门与金蝶云星空对接集成订单查询连通销售订单新增(旺店通销售-金蝶销售订单-小红书)
  • 卡尔曼滤波应用在数据处理方面的应用
  • PROFIBUS主站转ETHERCAT协议网关
  • Vue路由的使用及node.js下载安装和环境搭建
  • 【算法训练-二叉树 三】【最大深度与直径】求二叉树的最大深度、求二叉树的直径
  • 查看linux是centos还是Ubuntu
  • win10怎么关闭自动更新,这个方法你知道吗?
  • 「语音芯片」常见的OTP芯片故障分析
  • 孩子写作业买什么样台灯合适?适合孩子读写台灯推荐
  • DBAPI插件开发指南
  • 线程池使用之自定义线程池
  • Puppeteer无头浏览器:开启自动化之门,掌握浏览器世界的无限可能
  • Ubuntu 23.10/24.04 LTS 放弃默认使用 snap 版 CUPS 打印堆栈
  • Linux CentOS7 history命令
  • XC5350A 单节锂电池保护芯片 过放2.9V/2.8V/2.4V保护IC