leetcode 200 : Number of Islands

leetcode解题之200. Number of Islands Java版(岛屿的数量) leetcode解题之200. Number of Islands Java版(岛屿的数量) ,深度优先遍历 阅读详情

Number of Islands

Total Accepted: 8305 Total Submissions: 38192

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:

11110
11010
11000
00000

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3

[思路]

这道题有点像 word search的简化版. 每遇到'1'后, 开始向四个方向 递归搜索. 搜到后变为'0', 因为相邻的属于一个island. 然后开始继续找下一个'1'.

[CODE]

public class Solution {
    public int numIslands(char[][] grid) {
        int count = 0;
        for(int i=0; i<grid.length; i++) {
            for(int j=0; j<grid[0].length; j++) {
                if(grid[i][j]=='1') {
                    search(grid, i, j);
                    ++count;
                }
            }
        }
        return count;
    }
    
    private void search(char[][] grid, int x, int y) {
        if(x<0 || x>=grid.length || y<0 || y>=grid[0].length || grid[x][y]!='1') return;
        grid[x][y] = '0';
        search(grid, x-1, y);
        search(grid, x+1, y);
        search(grid, x, y-1);
        search(grid, x, y+1);
    }
}


leetcode-200-岛屿问题 如果要改变数组的内容,要函数传参要用引用的形式,不要直接传值,不然会浪费很多时间。 阅读详情

相关推荐

LLM的MCP协议:初识MCP,搭建第一个MCP Server

MCP(Model Context Protocol),是一个开发的协议,标准化了应用程序如何为大模型提供上下文。MCP提供了一个标准的为LLM提供数据、工具的方式,使用MCP会更容易的构建Agent或者是基于LLM的复杂工作流。

Criss@陈磊 2万+

LeetCode200. Number of Islands 岛屿数量

LeetCode200. Number of Islands 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/number-of-isla...

负雪明烛 1万+

微信小程序 - {errCode: 10008, errno: 1500104, errMsg: “writeBLECharacteristicValue:fail write characteris

微信小程序errCode: 10008, errno: 1500104, errMsg: "writeBLECharacteristicValue:fail write characteristics error. 10008, errMsg=The value's length is invalid.",uni-app微信小程序报错,uniApp小程序调用蓝牙api报错,向低功耗蓝牙设备的某个特征写入数据时错误,UniApp微信小程序蓝牙API调用报错10008解决方案,蓝牙设备进行通信时,向设备写入数据

🏆 前端领域优质创作者 4381

LeetCode题解–200. Number of Islands

链接LeetCode题目:https://leetcode.com/problems/number-of-islands/难度:Medium题目 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 form

sinat_14867719的博客 478

LeetCode 200. Number of Islands

LeetCode 200. Number of Islands (Medium) 主要知识点:二维DFS、二维BFS、Union Find;优先级:5;

温迪今天刷题了吗 325

Leetcode —— 200. 岛屿数量(Java)

题目描述 给你一个由'1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。 岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。 此外,你可以假设该网格的四条边均被水包围。 示例​​ 题解 DFS。 遍历整个矩阵,当遇到grid[i][j] == '1'时,从此点开始做深度优先搜索dfs,岛屿数count + 1且...

看了就会暴富的博客! 478

LeetCode 200:岛屿数量 Number of Islands

题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded...

爱写Bug 489

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

卷卷萌的博客 304

LeetCode刷题:200. Number of Islands

LeetCode刷题:200. Number of Islands 原题链接:https://leetcode.com/problems/number-of-islands/description/ Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is sur...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 700

leetcode 200: Number of Islands

Scan the whole graph, if a '1' is found, do BFS and change all '1's connected to that '1' to '0'. class Solution { public: int numIslands(vector>& grid) { if(grid.empty()) ret

mikefan1991的博客 305

Leetcode 200: 岛屿数量 Number of Islands

中文描述: 给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。 岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。 此外,你可以假设该网格的四条边均被水包围。 题目描述: 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

weixin_43946031的博客 328

leetcode 200Number of Islands

思路:深度优先搜索,两层循环遍历一遍grid,若该网格为1则进行深度优先搜索并将走过的结点的visit置1,记录连接分量的个数。 class Solution { public: void search(vector<vector<char>>& grid, int i, int j, vector<vector<int>&g...

weixin_30418341的博客 112

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 surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may a

sunwill的博客 545

200. Number of Islands(BFS)

https://leetcode.com/problems/number-of-islands/description/ 题目:求岛屿的数目,所有相连的1构成一个岛屿。 思路:BFS class Solution { public: int sum = 0; void BFS(vectorvectorchar>>& grid,int x,int y){ int

博客 571

Leetcode 200 岛屿数量(搜索 java)

如何设计深度优先搜索(DFS) 搜索到位置,进行一个标记,按照上下左右搜索,直到某一个点的深度遍历全都搜索完毕,岛屿数量加1 小象学院给的是使用一个辅助数组进行标记,其实用原先的一个数组也是可以的,遍历中心位之后,如果中心位是1,count++将中心位置为非1的数字就好了,我看网上有置为0的,我们为了和水域也分开比较,我们将遍历过的土地置为1. 我们 如何设计宽度优先...

yysave的专栏 1013

Leetcode200. Number of Islands

给定一个二维矩阵,只含。

数学、算法爱好者的博客 308

解法相似-leedcode200岛屿问题---面试题13机器人运动问题

dfs的相似题目

m0_51998128的博客 133

java数据结构与算法刷题-----LeetCode200. 岛屿数量

【代码】java数据结构与算法刷题-----LeetCode200. 岛屿数量。

殷志鹏的博客 639

BK7258 Datasheet-V1.4.pdf-规格书

BK7258是一款高度集成的1x1单波段2.4 GHz Wi-Fi 6 (802。11b/g/n/ax)和蓝牙5.4低能耗(LE)组合解决方案,设计用于需要高安全性和丰富资源的应用程序。集成了一个32位的ARMv8-M Star单片机和一套全面的外设,使BK7258成为高级物联网(IoT)应用程序的理想选择。BK7258提供了基于一个强大的安全架构的最先进的安全性。BK7258集成了一个物联网平台安全套件(用于加密和系统安全控制的IPSS)。IPSS嵌入了全面和健壮的安全特性,为物联网设备建立了一个绝密的执行环境。使用先进的设计技术和超低功耗处理技术,BK7258为IP相机、HMI应用、智能锁和其他高级物联网应用提供了高集成性、高效的安全性和最小的功耗。

上一篇: leetcode 210: Course Schedule II
下一篇: leetcode 201: Bitwise AND of Numbers Range
xudli
博客等级 码龄18年 204粉丝 275原创
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值