当前位置:网站首页>LeetCode56. 合并区间
LeetCode56. 合并区间
2022-06-28 20:59:00 【Yuyy】
本文最后更新于 484 天前,其中的信息可能已经有所发展或是发生改变。
一、思路
通过画图找规律 区间问题先排序
二、问题
以数组 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 <= 104intervals[i].length == 20 <= starti <= endi <= 104
Related Topics
- 排序
- 数组
\n
- 813
- 0
三、代码
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, (o1, o2) -> {
return o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0];
});
int start = intervals[0][0];
int end = intervals[0][1];
int[][] result = new int[intervals.length][2];
ArrayList<int[]> ans = new ArrayList<>();
for (int i = 0; i < intervals.length; i++) {
if (intervals[i][0] > end) {
ans.add(new int[]{start, end});
start = intervals[i][0];
end = intervals[i][1];
} else if (intervals[i][1] > end) {
end = intervals[i][1];
}
}
ans.add(new int[]{start, end});
return ans.toArray(new int[ans.size()][]);
}Post Views: 213
边栏推荐
- Anr no response introduction
- oracle delete误删除表数据后如何恢复
- Leetcode daily question - 30 Concatenate substrings of all words
- Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
- LeetCode每日一题——324. 摆动排序 II
- The blocks problem (uva101) Purple Book p110vector application
- Explanation of memory dump triggered by software watchdog and anr
- 方 差 分 析
- Understand the construction of the entire network model
- Please allow the "imperfection" of the current domestic Tob
猜你喜欢

How to analyze the relationship between enterprise digital transformation and data asset management?

视频号如何下载视频?来看超简单方法!

接口用例设计

Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)

Analysis of variance

Data standardization processing

Apisik helps Middle East social software realize localized deployment
随机推荐
LeetCode每日一题——324. 摆动排序 II
Flask——总结
应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)
Leetcode daily question - Sword finger offer II 091 Paint the house
阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
炒股票能赚钱么?开户安全嘛
Globalsign's Pan domain SSL certificate
The further application of Li Kou tree
Keyword long
LeetCode每日一题——515. 在每个树行中找最大值
with torch.no_grad():的使用原因
MySQL system error occurred 1067
软件watchdog和ANR触发memory dump讲解
Data standardization processing
postman简介与安装步骤
Flask - Summary
视频号如何下载视频?来看超简单方法!
API 网关 Apache APISIX 助力雪球双活架构演进
接口测试流程