当前位置:网站首页>数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
2022-06-27 15:41:00 【小型骷髅】
力扣热题100之第56题:合并区间

先贴代码:
class Solution {
public int[][] merge(int[][] intervals) {
// 先对 intervals 数组进行排序
Arrays.sort(intervals, (v1, v2) -> v1[0] - v2[0]);
// 创建数组 list 存储合并后的区间
List<int[]> list = new ArrayList<int[]>();
for (int i = 0; i < intervals.length; i++) {
int L = intervals[i][0], R = intervals[i][1];
if (list.size() == 0 || list.get(list.size() - 1)[1] < L) {
list.add(new int[]{L, R});
} else {
list.get(list.size() - 1)[1] = Math.max(list.get(list.size() - 1)[1], R);
}
}
return list.toArray(new int[list.size()][]);
}
}解题思路:
本题中要求我们将所有可合并的区间全部合并,可合并的区间啥意思?就是这两个区间如果有交集,那么就属于可合并的区间。例如题目当中的例子:[ [1,3] , [2,6] , [8,10] , [15,18] ],在水平坐标线上表示如下:

可以看到,在以上四个区间中,只有红色区间和绿色区间有重叠部分,即有交集,那么说明红色区间 [1,3] 和绿色区间 [2,6] 是可合并的,合并后的区间为 [1,6] 。
了解完题意之后,这一题又该如何解答呢?
我们先来了解以一下两个区间可合并是有什么条件,因为上图可知,当两个区间有交集时,是可合并的,而且当两个区间,一个区间是另一个区间的子集时,这两个区间也是可合并的。所以当两个区间可合并时,条件就为前一个区间的最大值大于后一个区间的最小值,或者是一个区间包含另一个区间。由此我们可以判定,当两个区间的最小值按由小到大排列时,只判断最大值的大小即可。
由此可以获得解题思路,先将数组里的区间按照最小值由小到大排列,这样如果两个区间可合并,那么这两个区间一定是连续的!由于题目中的数组 intervals 是二维数组,所以在排序时需要用到 lambda 表达式:

也可以简写为:

这样就可以让 intervals 数组按照区间的最小值排列,然后我们再新建一个 list 来存储合并后的 区间。
边栏推荐
- ORM表关系及操作
- Design of FIR digital filter
- VS编译遇到的问题
- 【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)
- If you want to use DMS to handle database permissions, can you only use Alibaba cloud ram accounts (Alibaba cloud RDS)
- NFT双币质押流动性挖矿dapp合约定制
- Nemo of pulseaudio (22)
- NFT dual currency pledge liquidity mining DAPP contract customization
- [kotlin] the next day
- 鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态
猜你喜欢

Li Chuang EDA learning notes 16: array copy and array distribution

洛谷入门1【顺序结构】题单题解

基于 Nebula Graph 构建百亿关系知识图谱实践

E modulenotfounderror: no module named 'psychopg2' (resolved)

Numerical extension of 27es6

【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)

字节跳动埋点数据流建设与治理实践

#28对象方法扩展

Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear

Google Earth Engine(GEE)——Export. image. The difference and mixing of toasset/todrive, correctly export classification sample data to asset assets and references
随机推荐
设计原则和思想:设计原则
VS编译遇到的问题
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
If you want to use DMS to handle database permissions, can you only use Alibaba cloud ram accounts (Alibaba cloud RDS)
The latest development course of grain college in 2022: 8 - foreground login function
ICML 2022 ぷ the latest fedformer of the Dharma Institute of Afghanistan ⻓ surpasses SOTA in the whole process of time series prediction
锚文本大量丢失的问题
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
A distribution fission activity is more than just a circle of friends!
Julia constructs diagonal matrix
What is the open source compatibility of the current version of polardb-x? mysql8?
[kotlin] the next day
Li Chuang EDA learning notes 16: array copy and array distribution
Source NAT address translation and server mapping web page configuration of firewall Foundation
LeetCode每日一练(杨辉三角)
Distributed session solution
A distribution fission activity is more than just a circle of friends!
事务的四大特性