当前位置:网站首页>数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【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 来存储合并后的 区间。
边栏推荐
- Openssf security plan: SBOM will drive software supply chain security
- Source NAT address translation and server mapping web page configuration of firewall Foundation
- Format of mobile number
- 创建数据库并使用
- Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
- OpenSSF安全计划:SBOM将驱动软件供应链安全
- #27ES6的数值扩展
- Basic configuration and usage of Jupiter notebook
- Leetcode daily practice (main elements)
- IDE Eval reset unlimited trial reset
猜你喜欢

LeetCode每日一练(主要元素)

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

Open source 23 things shardingsphere and database mesh have to say

VS编译遇到的问题

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊

数据中心表格报表实现定制统计加班请假汇总记录分享

2022年最新《谷粒学院开发教程》:8 - 前台登录功能

Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology

Source NAT address translation and server mapping web page configuration of firewall Foundation

#27ES6的数值扩展
随机推荐
The latest development course of grain college in 2022: 8 - foreground login function
一场分销裂变活动,不止是发发朋友圈这么简单!
Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration
一场分销裂变活动,不止是发发朋友圈这么简单!
SIGKDD22|图“预训练、提示、微调”范式下的图神经网络泛化框架
带你认识图数据库性能和场景测试利器LDBC SNB
About tensorflow using GPU acceleration
What should the ultimate LAN transmission experience be like
设计原则和思想:设计原则
Pisa-Proxy 之 SQL 解析实践
Redis系列2:数据持久化提高可用性
事件监听机制
Luogu_ P1003 [noip2011 improvement group] carpet laying_ Violence enumeration
事务的隔离级别详解
Design of CAN bus controller based on FPGA (with main codes)
PolarDB-X现在版本的开源兼容什么?mysql8?
The array of C language is a parameter to pass a pointer
ICML 2022 | 阿⾥达摩院最新FEDformer,⻓程时序预测全⾯超越SOTA
PolarDB-X开源版有没有支持 mysql5.7 的版本?
PSS:你距离NMS-free+提点只有两个卷积层 | 2021论文