LeetCode Merge Intervals
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18]
,
return [1,6],[8,10],[15,18]
.
题意:给出一些区间段,求其合并后的区间段
思路:排序时,[a,b]首先a最小的排在前面,如果起始相等,就以b最小的排在前面。在合并时。首先以第一个作为第一个区间段,如果下一个区间段的起始位置位于前一个区间段之间,就更新end。
代码如下:
class Solution {public List<Interval> merge(List<Interval> intervals){if (intervals.isEmpty())return intervals;Comparator<Interval> cmp = new Comparator<Interval>() {public int compare(Interval a, Interval b) {if (a.start != b.start)return a.start - b.start;return a.end - b.end;}};List<Interval> res = new ArrayList<Interval>();Collections.sort(intervals, cmp);int size = intervals.size();int start = intervals.get(0).start;int end = intervals.get(0).end;for (int i = 1; i < size; i++){Interval tmp = intervals.get(i);if (tmp.start >= start && tmp.start <= end){end = Math.max(tmp.end, end);}else{Interval interval = new Interval(start, end);res.add(interval);start = tmp.start;end = tmp.end;}}res.add(new Interval(start, end));return res;}
}
LeetCode Merge Intervals相关推荐
- [LeetCode]Merge Intervals
题目:Merge Intervals 给定n个区间合并重合区间 思路: 先按照区间起点排序,然后合并下面情况: 1.起点相同,以最大的终点为新的终点: 2.前一个终点大于后一个的起点. /****** ...
- [leetcode]Merge Intervals @ Python
原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...
- LeetCode:Merge Intervals
题目链接 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6 ...
- LeetCode - Merge Intervals
02.08.2021 小白一枚,文章仅当日记本记录学习进度 ;) 如被浏览,请多多指教,非常感谢大家纠错与建议! (因留学顺便练习英语,所以部分用英文笔记,并无他意) Solution 1 - sor ...
- 【细节实现题】LeetCode 56. Merge Intervals
LeetCode 56. Merge Intervals Solution1:我的答案 这道题思路不难,有个坑爹的地方在于输入的区间向量未必是有序的,所以利用优先队列priority_queue来排序 ...
- 【leetcode】56. Merge Intervals 相邻线段归并
1. 题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2, ...
- C#LeetCode刷题之#56-合并区间(Merge Intervals)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3676 访问. 给出一个区间的集合,请合并所有重叠的区间. 输入: ...
- 56. Merge Intervals(合并区间)解法(C++ 注释)
56. Merge Intervals(合并区间) 1. 题目描述 2. 排序(Sorting) 2.1 解题思路 2.2 实例代码 1. 题目描述 给出一个区间的集合,请合并所有重叠的区间. 示例 ...
- LeetCode 56. Merge Intervals
原题链接在这里:https://leetcode.com/problems/merge-intervals/ 题目: Given a collection of intervals, merge al ...
最新文章
- CornerNet-Lite:CornerNet粗暴优化,加速6倍还提点了 | BMVC 2020
- [bzoj2229][Zjoi2011]最小割
- 1100 Mars Numbers (20 分)【难度: 一般 / 知识点: 模拟】
- 【JFreeChart】JFreeChart简介
- WordPress里的PHP AJAX call
- YbtOJ#20236-[冲刺NOIP2020模拟赛Day9]红点蓝点【线段树,堆】
- [渝粤教育] 中国地质大学 国际贸易理论 复习题 (2)
- 【LeetCode】——根据前序和中序遍历构建二叉树
- vue cli3 搭建项目 使用vue-router 以及 element-ui
- 10录屏电流声_到底有没有完全免费的电脑+手机(安卓)录屏神器推荐,要能录制系统内部声音+麦克风的?...
- Struts1和Struts2的特点、工作流程及差异性
- flashftp中文绿色破解版免费下载
- Unity编辑器拓展--Hierarchy拓展
- c51流水灯实验报告汇编语言,单片机流水灯实验
- 最美遇见最好的你,感谢!
- 计算机网线怎么连接另一台电脑,求解一台电脑怎么连接另一台电脑上网
- 利用$randon和seed可以在测试脚本里面产生测试所需的赋值
- 电脑开机时stage 1 fo 3 stage 2 fo 3 Checking file system on D:
- 骨传导耳机有哪些推荐?真骨传导蓝牙耳机推荐
- 1.3 C++常量 (Constants )
热门文章
- 区别ie8和ie9的方法
- spring依赖注入单元测试:expected single matching bean but found 2
- linux内核如何安装vim,如何在Linux 中安装和使用 PacVim?
- shell启动java_一个通用的java Application启动shell脚本
- python这个软件学会能做什么工作-学会Python真的有高收入?盯,请查收这份入坑指南...
- 零基础python必背代码-编程零基础应当如何开始学习 Python?
- python天天学怎么样-每天一遍,好好学习,天天向上(Python)
- python与excel的应用-Python利用pandas处理Excel数据的应用
- python恶搞表情包-用 Python 把你的朋友变成表情包
- python画椭圆-python opencv圆、椭圆与任意多边形的绘制实例详解