当前位置:网站首页>[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
边栏推荐
- Basic knowledge of cryptography
- 6.12 企业内部upp平台(Unified Process Platform)的关键一刻
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- 13_ Redis_ affair
- 5. Practice: jctree implements the annotation processor at compile time
- 15_ Redis_ Redis. Conf detailed explanation
- I made an istio workshop. This is the first introduction
- 20_Redis_哨兵模式
- 21_Redis_浅析Redis缓存穿透和雪崩
- 17_Redis_Redis发布订阅
猜你喜欢

YOLOV5 代码复现以及搭载服务器运行

19_Redis_宕机后手动配置主机

FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA

MySQL -- Index Optimization -- order by

02_ Linear table_ Sequence table

6.12 critical moment of Unified Process Platform

18_Redis_Redis主从复制&&集群搭建

自定义异常

Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language

I made an istio workshop. This is the first introduction
随机推荐
【Leetcode】167-两数之和II -输入有序数组
10_ Redis_ geospatial_ command
LeetCode刷题——奇偶链表#328#Medium
4. Data splitting of Flink real-time project
Basic knowledge of cryptography
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
How to avoid 7 common problems in mobile and network availability testing
13_Redis_事务
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
高考录取分数线爬虫
高考分数线爬取
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
Bing. Site Internet
04_ Stack
Kibana basic operation
你不知道的Set集合
党史纪实主题公益数字文创产品正式上线
LeetCode刷题——两整数之和#371#Medium
MD5 encryption
15_ Redis_ Redis. Conf detailed explanation