当前位置:网站首页>[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
边栏推荐
- folium,确诊和密接轨迹上图
- List set & UML diagram
- How to choose a third-party software testing organization for automated acceptance testing of mobile applications
- 03. Preliminary use of golang
- Bing. Site Internet
- 04.进入云原生后的企业级应用构建的一些思考
- 让您的HMI更具优势,FET-G2LD-C核心板是个好选择
- Markdown tutorial
- 百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
- Yolov5 code reproduction and server operation
猜你喜欢

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

Custom exception

MySQL calculate n-day retention rate

4. Jctree related knowledge learning

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

SQL stored procedure

14_ Redis_ Optimistic lock

10_ Redis_ geospatial_ command

Loss function and positive and negative sample allocation: Yolo series

YOLOV5 代码复现以及搭载服务器运行
随机推荐
党史纪实主题公益数字文创产品正式上线
Leetcode skimming -- count the number of numbers with different numbers 357 medium
【LeetCode】977-有序數組的平方
18_Redis_Redis主从复制&&集群搭建
. Net again! Happy 20th birthday
11_ Redis_ Hyperloglog_ command
How to find a sense of career direction
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
folium,确诊和密接轨迹上图
MySQL calculate n-day retention rate
Solution of Queen n problem
LeetCode_ String_ Simple_ 412.Fizz Buzz
10_Redis_geospatial_命令
folium地图无法显示的问题,临时性解决方案如下
10_ Redis_ geospatial_ command
LeetCode刷题——递增的三元子序列#334#Medium
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
NBA player analysis
【LeetCode】1020-飞地的数量
16_ Redis_ Redis persistence