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 ...
随机推荐
- mybatis - [09] 动态SQL
题记部分 一.if & test 如果id,name,age不为空,则按照指定的值进行查询.如果这三者都是空(null和空字符串),则该sql执行结果为全表查询的结果集. <select ...
- C# Attribute 特性
https://blog.csdn.net/FantasiaX/article/details/1636913
- 红队单兵渗透工具-DudeSuite
声明!本文章所有的工具分享仅仅只是供大家学习交流为主,切勿用于非法用途,如有任何触犯法律的行为,均与本人及团队无关!!! DudeSuite 红队单兵作战渗透测试工具 DudeSuite(Dude S ...
- Easyexcel(5-自定义列宽)
注解 @ColumnWidth @Data public class WidthAndHeightData { @ExcelProperty("字符串标题") private St ...
- 【Python-Json】自定义类输入json序列化、json的读取与写入
AI 问答 Question json支持numpy数组么 Answer 不幸的是,标准的 JSON格式 不直接支持 NumPy 数组.JSON是一种用于存储和交换数据的文本格式,它有限的数据类型只包 ...
- 基于近红外与可见光双目摄像头的人脸识别与活体检测,文末附Demo
基于近红外与可见光双目摄像头的活体人脸检测原理 人脸活体检测(Face Anti-Spoofing)是人脸识别系统中的重要一环,它负责验证捕捉到的人脸是否为真实活体,以抵御各种伪造攻击,如彩色纸张打印 ...
- docker clean images
docker ps | grep portal | awk '{print $2}' | cut -d ":" -f3 used=`docker ps | grep portal ...
- Noise——随机之美
本篇博文介绍图形学中噪音生成的一般方法. Noise可以干什么? 不规则表面生成 有机体模拟 流体烟雾模拟 甚至是使用noise对灯光强度,位置做扰动: 只有我们想象不到的,没有noise不能涉猎的! ...
- leetcode每日一题:最小化字符串长度
题目 2716. 最小化字符串长度 给你一个下标从 0 开始的字符串 s ,重复执行下述操作 任意 次: 在字符串中选出一个下标 i ,并使 c 为字符串下标 i 处的字符.并在 i 左侧(如果有)和 ...
- study Python3【3】的函数
Python的函数定义简单,但灵活度非常大.功能强大意味复杂.为了复习,把廖雪峰老师的该课程做个回顾. 参数有:必选参数.默认参数.可变参数.关键字参数.命名关键字参数. 计算x的n次方函数: def ...