【Leetcode】【Hard】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].
解题思路:
1、将区间按照起始位置从小到大排序;
2、一次遍历,如果发现当前区间起始小于上一个区间结束,则进行合并;
解题步骤:
1、因为需要比较结构体,所以编写比较函数<
2、新建一个结果数组,保存合并后的结果;
3、对输入数组进行排序;
4、将第一个区间放入结果数组中;
5、从第二个区间开始遍历原数组:
(1)如果当前遍历到的区间start < 结果数组最后一个区间的end,则更改结果数组最后一个区间的end;
(2)否则,将当前遍历到的区间插入结果数组中;
代码:
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
static bool comp(const Interval& a, const Interval& b){
return a.start < b.start;
} vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> result;
if(intervals.empty()) {
return result;
}
sort(intervals.begin(), intervals.end(), comp);
result.push_back(intervals[]);
for(int i = ; i < intervals.size(); i++){
if(intervals[i].start <= result.back().end)
result.back().end = max(result.back().end, intervals[i].end);
else
result.push_back(intervals[i]);
} return result;
}
};
【Leetcode】【Hard】Merge Intervals的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- LeetCode解题报告—— Jump Game & Merge Intervals & Permutation Sequence
1. Jump Game Given an array of non-negative integers, you are initially positioned at the first inde ...
- 【LeetCode每天一题】Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- 【leetcode刷题笔记】Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- 【LeetCode每天一题】Merge Two Sorted Lists(合并两个排序链表)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
随机推荐
- pgadmin中的备份功能消失的原因和找回方法
pgadmin在正常情况下,在左侧的[对象浏览器]中选中数据库.模式或者表时,右键会出现[备份]和[恢复]的选项(除早期不支持该功能的版本外). 如: 当然,有时候有人会发现,当你想要用这些备份还原功 ...
- COleChangeSourceDialog不能Change Source的解决方法
在微软给的例子OClient中,有选中一个OLE对象然后Change Source的功能,但是会报错.分析了一下是这样的: void CMainView::OnOleChangeSource() ...
- asp.net web api 测试帮助页面建立并测试
asp.net web api 测试帮助页面建立并测试 现在使用WEB API来开发,越来越流行. 在开发过程中的测试调试,可以使用Fiddler等工具来帮助测试外,还有: 在asp.net 中有种方 ...
- eclipse颜色 字体
字体 之前也想过搜个字体,但是看了别人的推荐也没什么太大感觉.今天我自己选择了一款字体,发现看着单词中的i等瘦型的字母会比较费力,现在我知道自己想要什么样的字体了.网上推荐Inconsolata.pf ...
- socket编程相关的结构体和字节序转换、IP、PORT转换函数
注意:结构体之间不能直接进行强制转换, 必须先转换成指针类型才可以进行结构体间的类型转换, 这里需要明确的定义就是什么才叫强制转换. 强制转换是将内存中一段代码以另一种不同类型的方式进行解读, 因此转 ...
- 线程死锁情况和while在线程的作用
public class printDemo04 { public static void main(String[] args) { Resource01 resource01 = new Reso ...
- 获取 input 单选框和多选框的值
引用 jQuery的js <script> $(function(){ var arr = new Array(); $('#checkbox').click(function(){ a ...
- Swift3.0基础语法学习<四>
协议和扩展: // // ViewController4.swift // SwiftBasicDemo // // Created by 思 彭 on 16/11/16. // Copyright ...
- JProfiler使用入门(一)——准备工作
JProfiler是一个全功能的Java剖析工具(profiler),主要用于检查和跟踪系统(限于Java开发的)的性能. JProfiler可以通过时时的监控系统的内存使用情况,随时监视垃圾回收 ...
- 快速排序(java实现)
快速排序 算法思想:基于分治的思想,是冒泡排序的改进型.首先在数组中选择一个基准点(该基准点的选取可能影响快速排序的效率,后面讲解选取的方法),然后分别从数组的两端扫描数组,设两个指示标志(lo指向起 ...