当前位置:网站首页>Topic38——56. 合并区间
Topic38——56. 合并区间
2022-06-27 11:41:00 【_卷心菜_】
题目:以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。
示例 1:
输入:intervals = [[1,3],[2,6],[8,10],[15,18]]
输出:[[1,6],[8,10],[15,18]]
解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
输入:intervals = [[1,4],[4,5]]
输出:[[1,5]]
解释:区间 [1,4] 和 [4,5] 可被视为重叠区间。
提示:
1 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti <= endi <= 104
class Solution {
public int[][] merge(int[][] intervals) {
//对二维数组进行排序
Arrays.sort(intervals, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if(a[0] < b[0] || (a[0] == b[0] && a[1] < b[1])) {
return -1;
} else if(a[0] == b[0] && a[1] == b[1]) {
return 0;
} else {
return 1;
}
}
});
List<int[]> newArr = new ArrayList<>();
newArr.add(intervals[0]);
int index = 0;
for(int i = 1; i < intervals.length; i++) {
if(intervals[i][0] <= newArr.get(index)[1]) {
newArr.get(index)[1] = Math.max(intervals[i][1], newArr.get(index)[1]);
} else {
newArr.add(intervals[i]);
index++;
}
}
return newArr.toArray(new int[newArr.size()][]);
}
}
边栏推荐
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
- How to modify a node_ Files in modules
- Interview shock 60: what will cause MySQL index invalidation?
- 手把手带你入门 API 开发
- c/s 架构
- Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022
- Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用VGAM包的vglm函数对有序多分类logistic回归模型进行平行性假设作检验
- alibaba jarslink
- Histrix工作原理
猜你喜欢
StarCraft's Bug King ia retired for 2 years to engage in AI, and lamented that it was inferior
Nifi from introduction to practice (nanny level tutorial) - identity authentication
等等, 怎么使用 SetMemoryLimit?
Youboxun attended the openharmony technology day to create a new generation of secure payment terminals
Jwas: a Bayesian based GWAS and GS software developed by Julia
Summary of qstype class usage (II)
Nvme2.0 protocol - new features
"24 of the 29 students in the class successfully went to graduate school" rushed to the hot search! Where are the remaining five?
Wait, how do I use setmemorylimit?
Histrix工作原理
随机推荐
R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据、指定第一个字段降序排序,第二字段不指定(默认升序排序)
C# wpf 实现撤销重做功能
Thinkphp6 interface limits user access frequency
After Jerry's sleep, the regular wake-up system continues to run without resetting [chapter]
Informatics Olympiad all in one 1332: [example 2-1] weekend dance
动态规划【三】(区间dp)石子合并
Unity shader learning (II) the first shader
Summary of qstype class usage (III)
The wonderful use of 0 length array in C language
[tcapulusdb knowledge base] Introduction to tcapulusdb general documents
matlab习题 —— 创建 50 行 50 列全零矩阵、全 1 矩阵、单位矩阵、对角矩阵,输出矩阵第135号元素。
杰理之IO 口中断使用注意事项【篇】
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
"Internet +" contest topic hot docking | I figure to understand 38 propositions of Baidu
dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
i. Construction of mx6ull C language environment
The R language uses the follow up The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The labels parameter adds label information t
Shell script learning notes
56. Core principle of flutter - flutter startup process and rendering pipeline
Usage of rxjs mergemap