当前位置:网站首页>[leetcode] 1254 - count the number of closed Islands
[leetcode] 1254 - count the number of closed Islands
2022-07-02 15:36:00 【Crisp ~】
Two dimensional matrix grid from 0 ( land ) and 1 ( water ) form . The island is made up of the largest 4 Connected in two directions 0 Group composed of , The closed island is a Completely from 1 Surround ( Left 、 On 、 Right 、 Next ) The island of .
Please return Close the island Number of .
Example 1:
Input : grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
Output : 2
explain :
The islands in the gray area are closed Islands , Because the island is completely surrounded by water ( Namely be 1 The area surrounds ).
Example 2:
Input : grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
Output : 1
Example 3:
Input : g rid = [[1,1,1,1,1,1,1],
[1,0,0,0,0,0,1],
[1,0,1,1,1,0,1],
[1,0,1,0,1,0,1],
[1,0,1,1,1,0,1],
[1,0,0,0,0,0,1],
[1,1,1,1,1,1,1]]
Output : 2
Tips :
- 1 <= grid.length, grid[0].length <= 100
- 0 <= grid[i][j] <=1
# Breadth first traversal
class Solution(object):
def closedIsland(self, grid):
n = len(grid)
m = len(grid[0])
landcount = 0
for i in range(n):
for j in range(m):
if grid[i][j]==0:
val = 1
grid[i][j]=1
queue=[]
queue.append((i,j))
while queue:
x,y = queue[0]
queue.pop(0)
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if not 0<=xx<n or not 0<=yy<m:
val=0
elif grid[xx][yy]==0:
grid[xx][yy]=1
queue.append((xx,yy))
landcount+=val
return landcount
# Depth-first traversal
class Solution(object):
def closedIsland(self, grid):
n = len(grid)
m = len(grid[0])
landcount = 0
for i in range(n):
for j in range(m):
if grid[i][j]==0:
val = 1
grid[i][j]=1
stack=[]
stack.append((i,j))
while stack:
x,y = stack[-1]
stack.pop()
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if not 0<=xx<n or not 0<=yy<m:
val=0
elif grid[xx][yy]==0:
grid[xx][yy]=1
stack.append((xx,yy))
landcount+=val
return landcount
边栏推荐
- 党史纪实主题公益数字文创产品正式上线
- SQL stored procedure
- 16_ Redis_ Redis persistence
- 17_ Redis_ Redis publish subscription
- LeetCode刷题——去除重复字母#316#Medium
- 面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
- Common English abbreviations for data analysis (I)
- 【网络安全】网络资产收集
- 18_ Redis_ Redis master-slave replication & cluster building
- Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
猜你喜欢
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
Evaluation of embedded rz/g2l processor core board and development board of Feiling
损失函数与正负样本分配:YOLO系列
Mavn builds nexus private server
Data analysis thinking analysis methods and business knowledge - business indicators
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
20_ Redis_ Sentinel mode
Application and practice of Jenkins pipeline
14_Redis_乐观锁
Download blender on Alibaba cloud image station
随机推荐
15_Redis_Redis.conf详解
【LeetCode】417-太平洋大西洋水流问题
Leetcode skimming - remove duplicate letters 316 medium
15_ Redis_ Redis. Conf detailed explanation
PTA 天梯赛习题集 L2-001 城市间紧急救援
Party History Documentary theme public welfare digital cultural and creative products officially launched
07_ Hash
11_Redis_Hyperloglog_命令
02_线性表_顺序表
13_ Redis_ affair
How to avoid 7 common problems in mobile and network availability testing
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
YOLOV5 代码复现以及搭载服务器运行
Leetcode question brushing - parity linked list 328 medium
Set set you don't know
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
Pytoch saves tensor to Mat file
03_ Linear table_ Linked list
04_ Stack
I made an istio workshop. This is the first introduction