当前位置:网站首页>【LeetCode】1905-统计子岛屿
【LeetCode】1905-统计子岛屿
2022-07-02 12:09:00 【酥酥~】
给你两个 m x n 的二进制矩阵 grid1 和 grid2 ,它们只包含 0 (表示水域)和 1 (表示陆地)。一个 岛屿 是由 四个方向 (水平或者竖直)上相邻的 1 组成的区域。任何矩阵以外的区域都视为水域。
如果 grid2 的一个岛屿,被 grid1 的一个岛屿 完全 包含,也就是说 grid2 中该岛屿的每一个格子都被 grid1 中同一个岛屿完全包含,那么我们称 grid2 中的这个岛屿为 子岛屿 。
请你返回 grid2 中 子岛屿 的 数目 。
示例 1:
输入: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
输出: 3
解释: 如上图所示,左边为 grid1 ,右边为 grid2 。
grid2 中标红的 1 区域是子岛屿,总共有 3 个子岛屿。
示例 2:
**输入:**grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
**输出:**2
**解释:**如上图所示,左边为 grid1 ,右边为 grid2 。
grid2 中标红的 1 区域是子岛屿,总共有 2 个子岛屿。
提示:
- m == grid1.length == grid2.length
- n == grid1[i].length == grid2[i].length
- 1 <= m, n <= 500
- grid1[i][j] 和 grid2[i][j] 都要么是 0 要么是 1 。
#广度优先遍历
class Solution(object):
def countSubIslands(self, grid1, grid2):
m = len(grid2)
n = len(grid2[0])
result = 0#统计子岛屿数量
for i in range(m):
for j in range(n):
if grid2[i][j] == 1:
queue = []
grid2[i][j] = 0
queue.append((i,j))
flag = 1#标志是否为子岛屿
while queue:
x,y = queue[0]
queue.pop(0)
if grid1[x][y] == 0:#如果在grid1中对应的位置为海洋,则该岛屿不是子岛屿
flag = 0
for xx,yy in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
if 0<=xx<m and 0<=yy<n and grid2[xx][yy]==1:
grid2[xx][yy] = 0
queue.append((xx,yy))
result += flag
return result
#深度优先遍历
class Solution(object):
def countSubIslands(self, grid1, grid2):
m = len(grid2)
n = len(grid2[0])
result = 0
for i in range(m):
for j in range(n):
if grid2[i][j] == 1:
stack = []
grid2[i][j] = 0
stack.append((i,j))
flag = 1
while stack:
x,y = stack[-1]
stack.pop()
if grid1[x][y] == 0:
flag = 0
for xx,yy in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:#四个方向遍历
if 0<=xx<m and 0<=yy<n and grid2[xx][yy]==1:
grid2[xx][yy] = 0
stack.append((xx,yy))
result += flag
return result
边栏推荐
- Data analysis thinking analysis methods and business knowledge - business indicators
- Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
- 【LeetCode】1020-飞地的数量
- Deploy tidb cluster with tiup
- 【LeetCode】344-反转字符串
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
- 03_线性表_链表
- 10_Redis_geospatial_命令
- 14_Redis_乐观锁
- Leetcode question brushing - parity linked list 328 medium
猜你喜欢
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
Map introduction
How to avoid 7 common problems in mobile and network availability testing
yolo格式数据集处理(xml转txt)
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
15_Redis_Redis.conf详解
08_ strand
18_Redis_Redis主从复制&&集群搭建
5. Practice: jctree implements the annotation processor at compile time
随机推荐
03. Preliminary use of golang
10_Redis_geospatial_命令
Loss function and positive and negative sample allocation: Yolo series
YOLOV5 代码复现以及搭载服务器运行
Beijing rental data analysis
LeetCode刷题——验证二叉树的前序序列化#331#Medium
19_ Redis_ Manually configure the host after downtime
16_ Redis_ Redis persistence
21_ Redis_ Analysis of redis cache penetration and avalanche
Pytoch saves tensor to Mat file
03_线性表_链表
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
【Leetcode】167-两数之和II -输入有序数组
LeetCode刷题——两整数之和#371#Medium
11_Redis_Hyperloglog_命令
Custom exception
How to intercept the value of a key from the JSON string returned by wechat?
LeetCode刷题——递增的三元子序列#334#Medium
JVM architecture, classloader, parental delegation mechanism