当前位置:网站首页>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 .
边栏推荐
- #yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)
- Eolink launched a support program for small and medium-sized enterprises and start-ups to empower enterprises!
- 3.4 fixed number of cycles II
- 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!
- Difference between special invoice and ordinary invoice
- The role of the symbol @ in MySQL
- tensorflow求解泊松方程
- QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
- 【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)
- The time of localdatetime type (2019-11-19t15:16:17) is queried with the time range of Oracle
猜你喜欢

Eolink launched a support program for small and medium-sized enterprises and start-ups to empower enterprises!
![Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration](/img/9f/64b0b83211bd1c615f2db9273bb905.png)
Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration

Google Earth Engine(GEE)——Export. image. The difference and mixing of toasset/todrive, correctly export classification sample data to asset assets and references

LeetCode每日一练(无重复字符的最长子串)

Numerical extension of 27es6

What is RPC

鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态

郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮

Hierarchical clustering and case analysis

树莓派初步使用
随机推荐
泰山OFFICE技术讲座:第一难点是竖向定位
PolarDB-X开源版有没有支持 mysql5.7 的版本?
E modulenotfounderror: no module named 'psychopg2' (resolved)
域名绑定动态IP最佳实践
Redis系列2:数据持久化提高可用性
Sigkdd22 | graph generalization framework of graph neural network under the paradigm of "pre training, prompting and fine tuning"
Relation and operation of ORM table
Design of direct spread spectrum communication system based on FPGA (with main code)
Source NAT address translation and server mapping web page configuration of firewall Foundation
3.1 simple condition judgment
基于 Nebula Graph 构建百亿关系知识图谱实践
New method of cross domain image measurement style relevance: paper interpretation and code practice
QT audio playback upgrade (7)
利用Redis实现订单30分钟自动取消
字节跳动埋点数据流建设与治理实践
The latest development course of grain college in 2022: 8 - foreground login function
What are the password requirements for waiting insurance 2.0? What are the legal bases?
PSS: you are only two convolution layers away from the NMS free+ point | 2021 paper
Weekly snapshot of substrate technology 20220411
实现简单的三D立方体自动旋转