当前位置:网站首页>【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
边栏推荐
- 二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
- Application of CDN in game field
- 19_ Redis_ Manually configure the host after downtime
- Leetcode skimming -- incremental ternary subsequence 334 medium
- 面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
- Deploy tidb cluster with tiup
- Principles, language, compilation, interpretation
- Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
- 05_ queue
- 4. Data splitting of Flink real-time project
猜你喜欢

05_队列

YOLOV5 代码复现以及搭载服务器运行

03_ Linear table_ Linked list

MySQL calculate n-day retention rate

彻底弄懂浏览器强缓存和协商缓存

Tidb data migration tool overview

05_ queue

14_Redis_乐观锁

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

Evaluation of embedded rz/g2l processor core board and development board of Feiling
随机推荐
Principles, language, compilation, interpretation
Pytorch 保存tensor到.mat文件
QML pop-up frame, customizable
LeetCode_ String_ Simple_ 412.Fizz Buzz
Tidb data migration scenario overview
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
I made an istio workshop. This is the first introduction
03.golang初步使用
Tidb data migration tool overview
Steps for Navicat to create a new database
SQL stored procedure
Bing.com網站
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
MD5 encryption
夏季高考文化成绩一分一段表
士官类学校名录
How does the computer set up speakers to play microphone sound
15_Redis_Redis.conf详解
JVM architecture, classloader, parental delegation mechanism