当前位置:网站首页>【LeetCode】1020-飞地的数量
【LeetCode】1020-飞地的数量
2022-07-02 12:09:00 【酥酥~】
给你一个大小为 m x n 的二进制矩阵 grid ,其中 0 表示一个海洋单元格、1 表示一个陆地单元格。
一次 移动 是指从一个陆地单元格走到另一个相邻(上、下、左、右)的陆地单元格或跨过 grid 的边界。
返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数量。
示例 1:
输入: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
输出: 3
解释: 有三个 1 被 0 包围。一个 1 没有被包围,因为它在边界上。
示例 2:
输入: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
输出: 0
解释: 所有 1 都在边界上或可以到达边界。
提示:
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 500
- grid[i][j] 的值为 0 或 1
#广度优先遍历
class Solution(object):
def numEnclaves(self, grid):
m = len(grid)
n = len(grid[0])
result = 0
for i in range(m):
for j in range(n):
if grid[i][j]==1:
queue = []
queue.append((i,j))
grid[i][j]=0
cnt = 1#统计每一块岛屿的陆地数量
flag = 1#标志岛屿是否为有在边界的陆地
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 not 0<=xx<m or not 0<=yy<n:
flag = 0#如果有陆地在边界,则这块岛屿的所有陆地都是是飞地
elif grid[xx][yy]==1:
queue.append((xx,yy))
grid[xx][yy]=0
cnt+=1
if flag:
result+=cnt
return result
#深度优先遍历
class Solution(object):
def numEnclaves(self, grid):
m = len(grid)
n = len(grid[0])
result = 0
for i in range(m):
for j in range(n):
if grid[i][j]==1:
stack = []
stack.append((i,j))
grid[i][j]=0
cnt = 1
flag = 1
while stack:
x,y = stack[-1]
stack.pop()
for xx,yy in [(x+1,y),(x-1,y),(x,y-1),(x,y+1)]:
if not 0<=xx<m or not 0<=yy<n:
flag = 0
elif grid[xx][yy]==1:
stack.append((xx,yy))
grid[xx][yy]=0
cnt+=1
if flag:
result+=cnt
return result
边栏推荐
- PHP method to get the index value of the array item with the largest key value in the array
- Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
- Yolov5 code reproduction and server operation
- 【网络安全】网络资产收集
- How to write sensor data into computer database
- 10_ Redis_ geospatial_ command
- XML Configuration File
- Leetcode skimming -- sum of two integers 371 medium
- MD5 encryption
- 11_Redis_Hyperloglog_命令
猜你喜欢

03_ Linear table_ Linked list

Leetcode skimming -- incremental ternary subsequence 334 medium

Application and practice of Jenkins pipeline

做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统

Leetcode question brushing - parity linked list 328 medium

LeetCode刷题——统计各位数字都不同的数字个数#357#Medium

语义分割学习笔记(一)

终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)

Steps for Navicat to create a new database

How does the computer set up speakers to play microphone sound
随机推荐
13_Redis_事务
02_ Linear table_ Sequence table
你不知道的Set集合
Practical debugging skills
How to conduct TPC-C test on tidb
Infra11199 database system
彻底弄懂浏览器强缓存和协商缓存
Application of CDN in game field
19_Redis_宕机后手动配置主机
16_Redis_Redis持久化
. Solution to the problem of Chinese garbled code when net core reads files
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
14_ Redis_ Optimistic lock
03_ Linear table_ Linked list
损失函数与正负样本分配:YOLO系列
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
5. Practice: jctree implements the annotation processor at compile time
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
Deploy tidb cluster with tiup
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!