当前位置:网站首页>Leetcode56. consolidation interval
Leetcode56. consolidation interval
2022-06-28 21:05:00 【Yuyy】
This paper is finally updated at 484 Days ago, , The information may have developed or changed .
One 、 Ideas
adopt drawing Looking for a regular Interval problem first Sort
Two 、 problem
In array intervals Represents a set of intervals , The single interval is intervals[i] = [starti, endi] . Please merge all overlapping intervals , And return a non overlapping interval array , The array needs to cover exactly all the intervals in the input .
Example 1:
Input :intervals = [[1,3],[2,6],[8,10],[15,18]]
Output :[[1,6],[8,10],[15,18]]
explain : Section [1,3] and [2,6] overlap , Combine them into [1,6].Example 2:
Input :intervals = [[1,4],[4,5]]
Output :[[1,5]]
explain : Section [1,4] and [4,5] Can be regarded as overlapping interval .Tips :
1 <= intervals.length <= 104intervals[i].length == 20 <= starti <= endi <= 104
Related Topics
- Sort
- Array
\n
- 813
- 0
3、 ... and 、 Code
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
边栏推荐
- LeetCode213. 打家劫舍II
- Is it safe to open a dig money account? Is it reliable?
- LeetCode226. 翻转二叉树
- RT-Thread线程同步与线程通信
- [graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!
- MySQL system error occurred 1067
- rapid ssl通配符证书八百一年是正版吗
- LeetCode56. 合并区间
- LeetCode116. 填充每个节点的下一个右侧节点指针
- 03.hello_ rust
猜你喜欢

Analysis of variance

The further application of Li Kou tree
![[try to hack] cobalt strike (I)](/img/2b/5d274078b7d7ebd05b7c6d9e020868.png)
[try to hack] cobalt strike (I)

API gateway Apache APIs IX helps the evolution of snowball dual active architecture

ThreadLocal principle

MongoDB——副本集与分片

阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践

Leetcode 36. Effective Sudoku (yes, once)

with torch. no_ Grad(): reason for using

Pie (poj3122) super detailed and easy to understand binary introduction
随机推荐
LeetCode986. 区间列表的交集
Leetcode daily question - 515 Find the maximum value in each tree row
Binary tree problems
图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
pyechart绘制多条y轴折线图
Input and output real data
我也差点“跑路”
Is it safe to open a dig money account? Is it reliable?
1. integrate servlets
【学习笔记】主成分分析法介绍
Anr no response introduction
如何添加 logs来debug ANR 问题
I almost ran away
Understanding of incomplete types
Resilience4j retry source code analysis and retry index collection
LeetCode122. 买卖股票的最佳时机II
ThreadLocal原理
RT thread thread synchronization and thread communication
接口用例设计
【Try to Hack】Cobalt Strike(一)