当前位置:网站首页>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 .
边栏推荐
- PolarDB-X现在版本的开源兼容什么?mysql8?
- Luogu_ P1003 [noip2011 improvement group] carpet laying_ Violence enumeration
- Does polardb-x open source support mysql5.7?
- What is the open source compatibility of the current version of polardb-x? mysql8?
- In the Alibaba cloud experiment, if the k8s forwards to port 3306 and the MySQL client is turned on, it will terminate abnormally. What is the reason?
- Can polardb-x be accessed through the client of related tools as long as the client supporting JDBC / ODBC protocol is afraid?
- Distributed session solution
- 3.2 multiple condition judgment
- About MySQL: the phenomenon and background of the problem
- If you want to use DMS to handle database permissions, can you only use Alibaba cloud ram accounts (Alibaba cloud RDS)
猜你喜欢
P.A.R.A 方法在思源的简易应用(亲测好用)

The latest development course of grain college in 2022: 8 - foreground login function

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing

分布式Session解决方案

开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
![[kotlin] the next day](/img/13/9040e72de1243e827045b4572b0cd9.png)
[kotlin] the next day
#yyds干货盘点#简述chromeV8引擎垃圾回收

A distribution fission activity is more than just a circle of friends!

Open source 23 things shardingsphere and database mesh have to say

Sigkdd22 | graph generalization framework of graph neural network under the paradigm of "pre training, prompting and fine tuning"
随机推荐
深耕数字化,引领云原生,服务更多开发者
In the Alibaba cloud experiment, if the k8s forwards to port 3306 and the MySQL client is turned on, it will terminate abnormally. What is the reason?
防火墙基础之源NAT地址转换和服务器映射web页面配置
Four characteristics of transactions
Regular matching starts with what, ends with what, starts with what, and ends with what
Hierarchical clustering and case analysis
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie
Li Chuang EDA learning notes 16: array copy and array distribution
PolarDB-X现在版本的开源兼容什么?mysql8?
The role of the symbol @ in MySQL
Pragma once Usage Summary
等保2.0密码要求是什么?法律依据有哪些?
QT audio playback upgrade (7)
正则匹配以什么开头、以什么结尾,以非什么开头,以非什么结尾
域名绑定动态IP最佳实践
Julia constructs diagonal matrix
[pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)
【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)
锚文本大量丢失的问题