当前位置:网站首页>【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
边栏推荐
- List set & UML diagram
- Map introduction
- 5. Practice: jctree implements the annotation processor at compile time
- 21_ Redis_ Analysis of redis cache penetration and avalanche
- 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
- 彻底弄懂浏览器强缓存和协商缓存
- 04. Some thoughts on enterprise application construction after entering cloud native
- 百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
- 08_ strand
- 02.面向容器化后,必须面对golang
猜你喜欢
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
08_ strand
Pytoch saves tensor to Mat file
Leetcode skimming -- incremental ternary subsequence 334 medium
LeetCode刷题——递增的三元子序列#334#Medium
Pytorch 保存tensor到.mat文件
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
16_Redis_Redis持久化
Kibana basic operation
Bing.com網站
随机推荐
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
17_Redis_Redis发布订阅
I made an istio workshop. This is the first introduction
Loss function and positive and negative sample allocation: Yolo series
Oracle primary key auto increment
Pytorch 保存tensor到.mat文件
Map introduction
. Net again! Happy 20th birthday
03. Preliminary use of golang
How to solve the problem of database content output
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
How to find a sense of career direction
20_ Redis_ Sentinel mode
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
夏季高考文化成绩一分一段表
Bing.com網站
17_ Redis_ Redis publish subscription
14_Redis_乐观锁
LeetCode刷题——递增的三元子序列#334#Medium
Redux——详解