当前位置:网站首页>Topic38——56. Consolidation interval
Topic38——56. Consolidation interval
2022-06-27 12:16:00 【_ Cabbage_】
subject : In array intervals Represents a set of intervals , The single interval is intervals[i] = [starti, endi] . Please merge all overlapping intervals , And back to An array of non overlapping intervals , 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 <= 104
intervals[i].length == 2
0 <= starti <= endi <= 104
class Solution {
public int[][] merge(int[][] intervals) {
// Sort two-dimensional arrays
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()][]);
}
}
边栏推荐
- hibernate操作oracle数据库 主键自增
- Tidb 6.0: making Tso more efficient tidb Book rush
- TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
- 面试突击60:什么情况会导致 MySQL 索引失效?
- 记一次 .NET 某物管后台服务 卡死分析
- Nvme2.0 protocol - new features
- 千万不要错过,新媒体运营15个宝藏公众号分享
- What is the TCP 3-time handshake process?
- R语言fpc包的dbscan函数对数据进行密度聚类分析、plot函数可视化聚类图
- R language uses the poisgof function of epidisplay package to test the goodness of fit of Poisson regression and whether there is overdispersion
猜你喜欢

2022ciscn central China Web

Interviewer: with the for loop, why do you need foreach?

一个有趣的网络掩码的实验

Nvme2.0 protocol - new features

nifi从入门到实战(保姆级教程)——身份认证

In 2021, the global carbon graphite brush revenue is about US $2366million, and it is expected to reach US $2701.8 million in 2028

How to find the movie and TV clips with the same lines? These 8 movies search for artifact, and find the corresponding segment in one line

剑指 Offer 04. 二维数组中的查找

AUTOCAD——三种修剪方式

Heap heap sort TOPK
随机推荐
剑指 Offer 04. 二维数组中的查找
Talk about go language and cloud native technology
聊聊 Go 语言与云原生技术
How to adjust an integer that is entered in Excel but always displays decimals?
Heap heap sort TOPK
Jianmu continuous integration platform v2.5.0 release
盘点一些好用且小众的 Markdown 编辑器
器审科普:创新医疗器械系列科普——胸骨板产品
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.labels参数在可视化图像中为强调线添加标签信息
Dynamic programming [4] (counting class DP) example: integer partition
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
threejs的环境光+点光源+平行光源+球面光 以及hepler理解+阴影()
数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
解压 .log.gz 文件
i.mx6ull(单片机) c语言环境搭建
内存四区(栈,堆,全局,代码区)
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用VGAM包的vglm函数对有序多分类logistic回归模型进行平行性假设作检验
Neo4j:入门基础(一)之安装与使用
星际争霸的虫王IA退役2年搞AI,自叹不如了