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 ...
随机推荐
- SpringCloud - [01] SpringCloud概述
题记部分 001 || 微服务概述 002 || 微服务技术栈 微服务条目 落地技术 服务开发 Spring.SpringMVC.SpringBoot 服务配置与管理 Netflix公司的Archai ...
- 2024NOIP邮寄
渺渺兮身外无物,无喜无悲无怖,不过是大梦一场,各自沉浮. 前言 原计划这篇游记兼总结是在考完后一天之内写出来(12.1 前),但是一方面是因为家里的笔记本插上 U 盘写不了东西,一方面是这次 NOIP ...
- Go红队开发—文件操作
目录 文件操作 创建目录 创建文件 获取File信息 文件重命名 删除文件 打开关闭文件 判断文件是否存在 判断文件是否有读取权限 复制文件 Read读取 ReadFull读取 ReadAtLeast ...
- CATIA速成
1.草图编辑器 1.指南针视图操作 指南针可以完成模型移动,旋转等视图操作 红色方点:移动指南针 白色圆点:视图旋转 指南针附着在部件上,操控部件旋转平移: 红色方点-移动.附着到部件上-视图操作.( ...
- 【ABAQUS 二次开发笔记】输出单元刚度矩阵
目录 相关的关键字 必须的参数 可选参数 使用关键字 输出到mtx文件 输出到dat文件 参考资料 相关的关键字 *ELEMENT MATRIX OUTPUT 此keyword用于将元素刚度矩阵和质量 ...
- redmine 用户没有状态内容
- uniapp 截屏扫码
最近开发功能遇到个需求,用户点击某个操作之后,需要截取当前屏幕内容,并扫码识别屏幕截图中的二维码,代码如下: 首先将代码抽离到外部文件中,以便复用: // 截图 export function tak ...
- [tldr] fish shell添加环境变量到配置文件
fish shell配置文件的编写格式和位置都和bash不同 文件位置 位于~/.config/fish/config.fish 设置PATH fish shell不会去读取~/.bashrc文件中的 ...
- Draw.io:你可能不知道的「白嫖级」图表绘制神器
介绍 draw.io 是一个在 GitHub 上开源且拥有近十年发展历史的成熟项目,它是一款用于绘制 UML 图表的工具. 如果你曾经为流程图的绘制而流泪,又或是在夜里和UML大战到失眠, 不妨试试它 ...
- DELPHI 检测服务器地址是否有效
利用DELPH 的ICMP控件检测服务器地址 function CheckNetServer():Boolean; begin IdIcmpClient1.Host := '192.168.1.230 ...