当前位置:网站首页>【LeetCode】200-岛屿数量
【LeetCode】200-岛屿数量
2022-07-02 12:09:00 【酥酥~】
给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
示例 1:
输入:
grid = [
[“1”,“1”,“1”,“1”,“0”],
[“1”,“1”,“0”,“1”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“0”,“0”,“0”]
]
输出: 1
示例 2:
输入: grid = [
[“1”,“1”,“0”,“0”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“1”,“0”,“0”],
[“0”,“0”,“0”,“1”,“1”]
]
输出: 3
提示:
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 300
- grid[i][j] 的值为 ‘0’ 或 ‘1’
注:
在遇到1就立刻置为0,不要等着从队列or栈中取出来之后再置为0,会导致大量坐标点被重复入队or入栈,使得时间超出限制!!!
##广度优先遍历
class Solution(object):
def numIslands(self, grid):
n = len(grid)#获取数组长度
if n == 0:
return 0
m = len(grid[0])#获取数组宽度
cnt = 0#岛屿数量
queue = []#队列容器
for i in range(n):
for j in range(m):
if grid[i][j] == "1":#如果遇见陆地就开始遍历
cnt+=1#岛屿数量加一
grid[i][j]="0"#置为0,以免重复访问
queue.append((i,j))#加入队列中,开始遍历
while queue:
x,y = queue[0]
queue.pop(0)
for xx,yy in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:#四个方向遍历
if 0<=xx<n and 0<=yy<m and grid[xx][yy]=="1":
grid[xx][yy]="0"
queue.append((xx,yy))
return cnt
##深度优先遍历
class Solution(object):
def numIslands(self, grid):
n = len(grid)
if n == 0:
return 0
m = len(grid[0])
cnt = 0
stack = []
for i in range(n):
for j in range(m):
if grid[i][j] == "1":
cnt+=1
grid[i][j]="0"
stack.insert(0,(i,j))
while stack:
x,y = stack[0]
stack.pop(0)
for xx,yy in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
if 0<=xx<n and 0<=yy<m and grid[xx][yy]=="1":
grid[xx][yy]="0"
stack.insert(0,(xx,yy))
return cnt
边栏推荐
- 自定义异常
- Leetcode skimming -- sum of two integers 371 medium
- Tidb data migration tool overview
- 工程师评测 | RK3568开发板上手测试
- The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
- List set & UML diagram
- Tidb environment and system configuration check
- Leetcode skimming -- incremental ternary subsequence 334 medium
- 02.面向容器化后,必须面对golang
- 17_Redis_Redis发布订阅
猜你喜欢
Case introduction and problem analysis of microservice
YOLOV5 代码复现以及搭载服务器运行
LeetCode刷题——递增的三元子序列#334#Medium
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
14_Redis_乐观锁
03. Preliminary use of golang
04_ Stack
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
SQL transaction
Bing. Site Internet
随机推荐
[solution] educational codeforces round 82
16_ Redis_ Redis persistence
03_线性表_链表
Force deduction solution summary 2029 stone game IX
Real estate market trend outlook in 2022
Pytorch 保存tensor到.mat文件
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
How does the computer set up speakers to play microphone sound
17_ Redis_ Redis publish subscription
Loss function and positive and negative sample allocation: Yolo series
Custom exception
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
Evaluation of embedded rz/g2l processor core board and development board of Feiling
15_Redis_Redis.conf详解
List set & UML diagram
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
Deploy tidb cluster with tiup