当前位置:网站首页>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()][]);
}
}
边栏推荐
- C/s architecture
- C語言0長度數組的妙用
- [tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction
- 器审科普:创新医疗器械系列科普——胸骨板产品
- Basic usage and principle of fork/join framework
- Jerry's serial port communication serial port receiving IO needs to set digital function [chapter]
- Unity Shader学习(一)认识unity shader基本结构
- 数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题
- In 2021, the global carbon graphite brush revenue is about US $2366million, and it is expected to reach US $2701.8 million in 2028
- Leetcode 177 The nth highest salary (June 26, 2022)
猜你喜欢

器审科普:创新医疗器械系列科普——胸骨板产品

Excel中输入整数却总是显示小数,如何调整?

Build the Internet of things system from scratch

Interview shock 60: what will cause MySQL index invalidation?

Rxjs mergeMap 的使用场合

Interview shock 60: what will cause MySQL index invalidation?

C语言0长度数组的妙用

动态规划【四】(计数类dp)例题:整数划分

StarCraft's Bug King ia retired for 2 years to engage in AI, and lamented that it was inferior

0 basic understanding of how e-commerce systems connect with payment channels
随机推荐
This privatized deployed enterprise knowledge base makes telecommuting a zero distance
Interview shock 60: what will cause MySQL index invalidation?
[tcapulusdb knowledge base] tcapulusdb system user group introduction
Thinkphp6 interface limits user access frequency
盘点一些好用且小众的 Markdown 编辑器
After Jerry's sleep, the regular wake-up system continues to run without resetting [chapter]
matlab习题 —— 创建 50 行 50 列全零矩阵、全 1 矩阵、单位矩阵、对角矩阵,输出矩阵第135号元素。
动态规划【四】(计数类dp)例题:整数划分
QStyle实现自绘界面项目实战(一)
Jerry's DAC output mode setting [chapter]
Mqtt protocol stack principle and interaction flow chart
旭日3SDB,安装原版ros
"Internet +" contest topic hot docking | I figure to understand 38 propositions of Baidu
Don't miss it. New media operates 15 treasure official account to share
[tcapulusdb knowledge base] tcapulusdb OMS business personnel permission introduction
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用dot.col参数指定分组数据点的颜色
JSP custom tag
56. Core principle of flutter - flutter startup process and rendering pipeline
聊聊 Go 语言与云原生技术
巅峰小店APP仿站开发玩法模式讲解源码分享