当前位置:网站首页>Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
2022-06-27 16:13:00 【Small skeleton】
Force deduction hot question 100 The first 56 topic : Merge range

Post the code first :
class Solution {
public int[][] merge(int[][] intervals) {
// First pair intervals Array to sort
Arrays.sort(intervals, (v1, v2) -> v1[0] - v2[0]);
// Create array list Store merged intervals
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()][]);
}
}Their thinking :
In this question, we are required to merge all the mergeable intervals , What do you mean by a range that can be merged ? If these two intervals intersect , Then it belongs to the mergeable interval . For example, the example in the title :[ [1,3] , [2,6] , [8,10] , [15,18] ], On the horizontal coordinate line, it is shown as follows :

You can see , In the above four intervals , Only the red section and the green section overlap , That is, there is intersection , So it means the red range [1,3] And the green zone [2,6] Is mergeable , The merged interval is [1,6] .
After understanding the meaning of the question , How to solve this problem ?
Let's first understand the conditions under which two intervals can be merged , Because the above figure shows , When two intervals intersect , Is mergeable , And when two intervals , When an interval is a subset of another interval , These two intervals can also be merged . So when two intervals can be merged , The condition is that the maximum value of the previous interval is greater than the minimum value of the next interval , Or an interval contains another interval . From this we can judge that , When the minimum values of two intervals are arranged from small to large , Just judge the size of the maximum value .
From this, we can get the idea of solving problems , First, arrange the intervals in the array from small to large according to the minimum value , So if two intervals can be merged , Then these two intervals must be continuous ! Because of the array in the title intervals Is a two-dimensional array , So you need to use... When sorting lambda expression :

Or we could just write it as :

So that we can intervals The array is arranged according to the minimum value of the interval , Then we'll create a new one list To store the merged Section .
边栏推荐
- Etcd可视化工具:Kstone部署(一),基于Helm快速部署
- 3.4 fixed number of cycles II
- #yyds干货盘点#简述chromeV8引擎垃圾回收
- NFT双币质押流动性挖矿dapp合约定制
- IDE Eval reset unlimited trial reset
- Hierarchical clustering and case analysis
- localDateTime类型的时间(2019-11-19T15:16:17) 用oracle的时间范围查询
- List to table
- Create a database and use
- Basic configuration and usage of Jupiter notebook
猜你喜欢
锚文本大量丢失的问题

LeetCode每日一练(两数之和)
![[pygame Games] ce jeu](/img/3c/e573106ec91441a554cba18d5b2253.png)
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)

SIGKDD22|图“预训练、提示、微调”范式下的图神经网络泛化框架

List转Table

substrate 技术每周速览 20220411

Redis系列2:数据持久化提高可用性

Construction and management practice of ByteDance buried point data flow

The interview lasted for half a year. Last month, I successfully got Alibaba p7offer. It was all because I chewed the latest interview questions in 2020!

Weekly snapshot of substrate technology 20220411
随机推荐
PolarDB-X现在版本的开源兼容什么?mysql8?
米哈游起诉五矿信托,后者曾被曝产品暴雷
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
SIGKDD22|图“预训练、提示、微调”范式下的图神经网络泛化框架
Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration
About tensorflow using GPU acceleration
QT5 之信号与槽机制(信号与槽的基本介绍)
Beginner level Luogu 2 [branch structure] problem list solution
3.1 simple condition judgment
分布式Session解决方案
Mobile terminal click penetration
跨域图像的衡量新方式Style relevance:论文解读和代码实战
Google Earth Engine(GEE)——Export. image. The difference and mixing of toasset/todrive, correctly export classification sample data to asset assets and references
Pisa-Proxy 之 SQL 解析实践
Does polardb-x open source support mysql5.7?
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
PSS:你距離NMS-free+提點只有兩個卷積層 | 2021論文
Distributed session solution