当前位置:网站首页>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()][]);
}
}
边栏推荐
- uniapp下拉弹层选择框效果demo(整理)
- MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)
- Topic38——56. 合并区间
- ACL 2022 | 中科院提出TAMT:TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
- Detailed explanation of interprocess communication
- 秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商
- 【面试高频题】难度 1.5/5,LCS 模板题
- TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
- MIT6.031 软件构造 Reading7阅读笔记Designing Specifications(设计规范)
- Interview shock 60: what will cause MySQL index invalidation?
猜你喜欢
StarCraft's Bug King ia retired for 2 years to engage in AI, and lamented that it was inferior
Interviewer: with the for loop, why do you need foreach?
Interview shock 60: what will cause MySQL index invalidation?
如何修改 node_modules 裏的文件
一个有趣的网络掩码的实验
Histrix工作原理
1. Mx6ull startup mode
pull request
Peak store app imitation station development play mode explanation source code sharing
动态规划【三】(区间dp)石子合并
随机推荐
Rxjs mergeMap 的使用场合
Safe landing practice of software supply chain under salesforce containerized ISV scenario
uniapp下拉弹层选择框效果demo(整理)
Deep understanding of happens before principle
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
C# wpf 实现撤销重做功能
c/s 架构
Popular science of device review: popular science of innovative medical device series - sternum plate products
master公式
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
R语言glm函数构建二分类logistic回归模型(family参数为binomial)、使用AIC函数比较两个模型的AIC值的差异(简单模型和复杂模型)
Dynamic programming [4] (counting class DP) example: integer partition
旭日3SDB,安装原版ros
ACL 2022 | 中科院提出TAMT:TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
uni-app 使用escook/request-miniprogram插件发请求说明
i.mx6ull(单片机) c语言环境搭建
Excel中输入整数却总是显示小数,如何调整?
Unity shader learning (I) understanding the basic structure of unity shader
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.labels参数在可视化图像中为强调线添加标签信息
FileOutputStream