当前位置:网站首页>[leetcode] 1020 number of enclaves
[leetcode] 1020 number of enclaves
2022-07-02 15:36:00 【Crisp ~】
Give you a size of m x n The binary matrix of grid , among 0 Represents an ocean cell 、1 Represents a land cell .
once Move It refers to walking from one land cell to another ( On 、 Next 、 Left 、 Right ) Land cells or across grid The boundary of the .
Return to grid unable The number of land cells that leave the grid boundary in any number of moves .
Example 1:
Input : grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output : 3
explain : There are three 1 By 0 Surround . One 1 Not surrounded , Because it's on the border .
Example 2:
Input : grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output : 0
explain : all 1 Are on the boundary or can reach the boundary .
Tips :
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 500
- grid[i][j] The value of is 0 or 1
# Breadth first traversal
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# Count the land quantity of each island
flag = 1# Mark whether the island is land with boundary
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# If there is land at the boundary , Then all the lands of this island are enclaves
elif grid[xx][yy]==1:
queue.append((xx,yy))
grid[xx][yy]=0
cnt+=1
if flag:
result+=cnt
return result
# Depth-first traversal
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
边栏推荐
- Evaluation of embedded rz/g2l processor core board and development board of Feiling
- Leetcode skimming - remove duplicate letters 316 medium
- 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
- 2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
- 百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
- 【LeetCode】876-链表的中间结点
- Bing.com网站
- Common English abbreviations for data analysis (I)
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- [solution] educational codeforces round 82
猜你喜欢
彻底弄懂浏览器强缓存和协商缓存
I made an istio workshop. This is the first introduction
【LeetCode】417-太平洋大西洋水流问题
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
02_ Linear table_ Sequence table
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
03.golang初步使用
Basic knowledge of cryptography
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
随机推荐
【网络安全】网络资产收集
. Net again! Happy 20th birthday
Pytoch saves tensor to Mat file
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
【LeetCode】1905-统计子岛屿
07_ Hash
03. Preliminary use of golang
夏季高考文化成绩一分一段表
Redux - detailed explanation
【LeetCode】1020-飞地的数量
17_ Redis_ Redis publish subscription
[network security] network asset collection
13_ Redis_ affair
Infra11199 database system
【LeetCode】977-有序数组的平方
08_ strand
How to write sensor data into computer database
List set & UML diagram
yolo格式数据集处理(xml转txt)
QML pop-up frame, customizable