Number of Islands——LeetCode进阶路
原题链接https://leetcode.com/problems/number-of-islands/
题目描述
Given a 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.
Example 1:
Input:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3
思路分析
这道题和上一题 “围绕的区域”https://blog.csdn.net/Moliay/article/details/88584035
思路相似,同用dfs,特定元素访问之后标记,递归出结果 bingo
源码附录
class Solution {
public int numIslands(char[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0)
{
return 0;
}
int row = grid.length;
int col = grid[0].length;
int result = 0;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(grid[i][j] == '1')
{
result ++;
dfs(grid,i,j,row,col);
}
}
}
return result;
}
public void dfs(char[][] grid,int i,int j,int row,int col)
{
if(i>row-1 || j>col-1 || i<0 || j<0)
{
return;
}
grid[i][j] = '2';
if(j<col-1 && grid[i][j+1] == '1')
{
dfs(grid,i,j+1,row,col);
}
if(j>0 && grid[i][j-1] == '1')
{
dfs(grid,i,j-1,row,col);
}
if(i<row-1 && grid[i+1][j] == '1')
{
dfs(grid,i+1,j,row,col);
}
if(i>0 && grid[i-1][j] == '1')
{
dfs(grid,i-1,j,row,col);
}
}
}
Number of Islands——LeetCode进阶路的更多相关文章
- Number of Islands——LeetCode
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Leetcode: Number of Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [leetcode]200. Number of Islands岛屿个数
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- LeetCode 305. Number of Islands II
原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...
- Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)
Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...
- LeetCode 200:岛屿数量 Number of Islands
题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...
- [LeetCode] 305. Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
随机推荐
- 使用QT开发远程linux服务器过程
1.添加设备为通用linux 2.设置ip用户名 3.创建私钥文件,原来有的qtc那俩个文件删掉. 4.部署公钥,前提是测试链接要出现成功 5.在kits里添加编译环境设置编译器为32位或者64 6. ...
- 0003 Failed to build the application: build go_beego/src/hello: cannot load
我使用beego框架快速建立了一个应用,可当我运行 bee run的时候,出现了如下错误 D:\go_beego\src\product>bee run ______ | ___ \ | |_/ ...
- 人工智能-A*算法-最优路径搜索实验
上次学会了<A*算法-八数码问题>,初步了解了A*算法的原理,本次再用A*算法完成一个最优路径搜索实验. 一.实验内容1. 设计自己的启发式函数.2. 在网格地图中,设计部分障碍物.3. ...
- 搭建自己的OCR服务,第三步:PPOCRLabel标注工具安装
一.安装说明 安装好了PaddleOCR后,还需要安装PPOCRLabel这个标注工具,想要自己训练模型的话,有个标注工具会起很大作用. 尤其是PPOCRLabel就是跟PaddleOCR配套的标注工 ...
- numpy -- 处理数值型数据 -- 数据分析三剑客
博客地址:https://www.cnblogs.com/zylyehuo/ NumPy(Numerical Python) 是 Python 语言中做科学计算的基础库.重在于数值计算,也是大部分Py ...
- linux服务器通过X11实现图形化界面显示 1 背景描述
有些LINUX服务器出于性能和效率的考虑,通常都是没有安装图形化界面的,那么图形化程序在服务器上压根儿就跑不起来,或者无法直接显示出来,这就很尴尬了!那么如何解决这个问题呢?可以基于X11 Forwa ...
- C# 多文件打包
public HttpResponseMessage GetZip() { var response = Request.CreateResponse(HttpStatusCode.OK); try ...
- 【硬件】认识和选购多核CPU
2.1 认识和选购多核CPU CPU在电脑系统中就像人的大脑一样,是整个电脑系统的指挥中心,电脑的所有工作都由CPU进行控制和计算.它的主要功能是负责执行系统指令,包括数据存储.逻辑运算.传输控制.输 ...
- openGL库环境简单配置
主要是针对openGL的一些初步的学习,因为openCV主要是处理图像视频,是从现有的得到数据,而openGL好像是从数据进行绘图,学习一下.在<计算机图形学编程>一书中,它把图形编程定性 ...
- Tomcat知识点整理
从学习起就开始接触tomcat, 解压, 点击运行, 然后放文件夹里面会自动部署, 可以通过ip访问. 在这里主要记录一些tomcat相关的知识点 配置文件解析(留位置) server.xml/web ...