当前位置:网站首页>[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
边栏推荐
- How to find a sense of career direction
- 【LeetCode】977-有序数组的平方
- Summary of the first three passes of sqli Labs
- Pytorch 保存tensor到.mat文件
- Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
- Bing. Com website
- 19_ Redis_ Manually configure the host after downtime
- 【LeetCode】877-石子游戏
- 14_Redis_乐观锁
- 11_Redis_Hyperloglog_命令
猜你喜欢
14_Redis_乐观锁
17_ Redis_ Redis publish subscription
14_ Redis_ Optimistic lock
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
03. Preliminary use of golang
Loss function and positive and negative sample allocation: Yolo series
LeetCode刷题——递增的三元子序列#334#Medium
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
Bing. Com website
Solve the problem of frequent interruption of mobaxterm remote connection
随机推荐
folium地图无法显示的问题,临时性解决方案如下
14_Redis_乐观锁
12_ Redis_ Bitmap_ command
07_ Hash
Semantic segmentation learning notes (1)
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
Custom exception
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
[leetcode] 695 - maximum area of the island
18_ Redis_ Redis master-slave replication & cluster building
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
[leetcode] 283 move zero
搭建自己的语义分割平台deeplabV3+
【LeetCode】1905-统计子岛屿
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
Markdown tutorial
SQL stored procedure
【LeetCode】417-太平洋大西洋水流问题
12_Redis_Bitmap_命令
Steps for Navicat to create a new database