当前位置:网站首页>leetcode:305. 岛屿的数量
leetcode:305. 岛屿的数量
2022-06-10 02:45:00 【OceanStar的学习笔记】
题目来源
题目描述
假设你设计一个游戏,用一个 m 行 n 列的 2D 网格来存储你的游戏地图。html
起始的时候,每一个格子的地形都被默认标记为「水」。咱们能够经过使用 addLand 进行操做,将位置 (row, col) 的「水」变成「陆地」。
你将会被给定一个列表,来记录全部须要被操做的位置,而后你须要返回计算出来 每次 addLand 操做后岛屿的数量。网络
注意:一个岛的定义是被「水」包围的「陆地」,经过水平方向或者垂直方向上相邻的陆地链接而成。
输入:m = 3, n = 3, positions = [[0,0],[0,1],[1,2],[2,1]]
输出:[1,1,2,3]
解释:
起初,二维网格 grid 被全部注入「水」。(0 代表「水」,1 代表「陆地」)
- 操作 #1:addLand(0, 0) 将 grid[0][0] 的水变为陆地。此时存在 1 个岛屿。
- 操作 #2:addLand(0, 1) 将 grid[0][1] 的水变为陆地。此时存在 1 个岛屿。
- 操作 #3:addLand(1, 2) 将 grid[1][2] 的水变为陆地。此时存在 2 个岛屿。
- 操作 #4:addLand(2, 1) 将 grid[2][1] 的水变为陆地。此时存在 3 个岛屿。

题目解析
并查集
和其他的不同是,这里的并查集是动态生成的
边栏推荐
- There is a bug in the new document, or it may be stuck
- Redis迭代查询详解及其使用:Scan命令、Sscan命令、Hscan命令、Zscan命令
- 正则表达式的边界问题。
- C#扩展方法(方法参数里的this)
- Mysql database foreign key foreing key
- JS sequence to obtain the distance between two places
- Catering e-procurement scheme: procurement cycle management, saving 30% of the cost
- Pat (advanced level) practice 1156 sex primes
- 51单片机入门——红外通信
- 高考【活动】
猜你喜欢

成为白帽大神 | 挑战 openGauss漏洞奖励计划

获取省市区的名称【项目 商城】

微信小程序 音乐播放代码(播放方式,歌词滚动)

Antinomy between brand and product

How to use Google home speaker voice to control zhiting home cloud devices?

midway的使用教程
MySQL 8.0.28 installation and configuration method graphic tutorial (compressed package method)

Technical dry goods | linkis1.0.2 installation and use guide

^27 timer related problems
Windows version MySQL 8.0.28 installation and configuration method graphic tutorial
随机推荐
Explain Seaborn in detail. Just read this one
2、自然语言处理入门
Lm03 who told you that cross species must be arbitraged?
The process of manually installing redis extensions in yii2 framework
Wechat applet music playing code (playing mode, lyrics scrolling)
command
51 Introduction au micro - ordinateur à puce unique - - communication infrarouge
JS sequence to obtain the distance between two places
Multi task learning
Example code of redis login registration
How to use Google home speaker voice to control zhiting home cloud devices?
Catering e-procurement scheme: procurement cycle management, saving 30% of the cost
MySQL installation + test connection + common terminal MySQL commands (super detailed)
uniapp 开发app时获取唯一标识OAID,imei,ooid
成为白帽大神 | 挑战 openGauss漏洞奖励计划
promise 介绍和实现
pixi. JS tile background
Pat (advanced level) practice 1001 a+b format (Analog)
Arduino与Processing串口通信(match函数)
双指针 | 283. 移动零