原题链接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进阶路的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. LeetCode 305. Number of Islands II

    原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...

  8. Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)

    Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...

  9. LeetCode 200:岛屿数量 Number of Islands

    题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...

  10. [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 ...

随机推荐

  1. 大数据之路Week08_day06 (Zookeeper初识)

    让我们来回顾一下我们在学习Hadoop中的HDFS的时候,肯定见过下面这样的两幅图: 这副图代表着什么呢?它介绍的是Hadoop集群的高可靠,也就是前面提过的HA,仔细观察一下这副图,我们发现有两个N ...

  2. 6. Calcite添加自定义函数

    1. 简介 在上篇博文中介绍了如何使用calcite进行sql验证, 但是真正在实际生产环境中我们可能需要使用到 用户自定义函数(UDF): 通过代码实现对应的函数逻辑并注册给calcite sql验 ...

  3. 少样本学习实战:Few-Shot Prompt设计

    让AI用最少样本学会"举一反三" 想象一下,你要教一个外星人认识地球上的动物.如果只给它看三张哈士奇的照片,它可能会认为所有四条腿的动物都叫"哈士奇".这就是A ...

  4. DeepSeek在M芯片Mac上本地化部署

    在 Mac 上使用 Ollama 运行 DeepSeek-R1,并通过 Open-WebUI 提供 Web 端访问. 1. 安装 Ollama Ollama官方:https://ollama.com/ ...

  5. Shell语言编程(炼气)

    1. Shell脚本执行方式 执行方式 应用及场景 通过sh或bash 书写脚本后,最常用的方式,在其他非红帽系统中,建议使用bash运行脚本 通过.点或source 加载/生效配置文件(环境变量,别 ...

  6. linux下nacos集群部署报错Error: Could not create the Java Virtual Machine.

    修改startup.sh时注意空格 注意这里-Dserver "-"之后没有空格 nohup $JAVA -Dserver.port=${PORT} ${JAVA_OPT} nac ...

  7. Effective Java理解笔记系列-第2条-何时考虑用构建器?

    为什么写这系列博客? 在阅读<Effective Java>这本书时,我发现有许多地方需要仔细认真地慢慢阅读并且在必要时查阅相关资料才能彻底搞懂,相信有些读者在阅读此书时也有类似感受:同时 ...

  8. 接口新特性--java进阶day03

    1.接口新特性 在JDk8和JDK9开始,接口可以定义普通方法 这时就会感到很奇怪,明明之前说好接口只是用来制定规则的,为什么现在又可以定义普通方法了呢? 我们以一个公司案例进行讲解,公司1.0上线了 ...

  9. HTB打靶记录-Administrator

    # 信息收集 nmap -sV -sC -O 10.10.11.42 Nmap scan report for 10.10.11.42 Host is up (0.70s latency). Not ...

  10. nodejs环境准备

    这是为了针对nodejs使用来进行的环境准备,分出windows和ubuntu两种情况: Windows 环境 安装 Node.js 下载安装包:访问下面nodejs官网: 选择适合 Windows ...