【leetcode】 Merge Intervals
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].
解题思路: 根据start对区间进行排序,然后依次遍历进行区间合并。
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool cmp(const Interval &a, const Interval &b){
return a.start < b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
if(intervals.size() == )
return intervals;
sort(intervals.begin(), intervals.end(), cmp);
vector<Interval> result;
Interval mover = intervals[];
for(int i = ; i < intervals.size(); ++i){
if(mover.end < intervals[i].start){
result.push_back(mover);
mover = intervals[i];
}else{
mover.end = max(mover.end, intervals[i].end);
}
}
result.push_back(mover);
return result;
}
};
【leetcode】 Merge Intervals的更多相关文章
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【Leetcode】【Hard】Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【LeetCode】Merge Sorted Array(合并两个有序数组)
这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nu ...
- 【leetcode】Merge Sorted Array
题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...
- 【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 ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【leetcode】Merge Two Sorted Lists(easy)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
随机推荐
- MyBatis 返回类型resultType为map时的null值不返回问题
问题一: 查询结果集中 某字段 的值为null,在map中不包含该字段的key-value对 解决:在mybatis.xml中添加setting参数 <!-- 在null时也调用 sett ...
- octave基本指令4
octave基本指令4 图形化显示数据 >> t=[0:0.01:0.98]; >> y1 = sin(2*pi*4*t); %pi表示π >> plot(t,y1 ...
- Oracle数据库SQLPLUS 连接显示 ??? 的解决
linux下 安装了中文版本的,造成sqlplus 连接时出现了乱码 如图 一开始以为是LANG 变量的问题 后来发现是NLS_LANG的问题 解决方法: export NLS_LANG=" ...
- [日常工作]vCenter下虚拟机设置与宿主机时间同步的方法
1. ESXi 能够实现CPU超售 同事开启多与CPU个数的虚拟机 不通的虚拟机采用了时间分片的处理, 所以有时候虚拟机内的时间可能会比宿主机的时间过的更慢, 越来越久之后虚拟机的时间就会比较离谱了. ...
- Angular factory自定义服务
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...
- C/C++的内存泄漏检测工具Valgrind memcheck的使用经历
Linux下的Valgrind真是利器啊(不知道Valgrind的请自觉查看参考文献(1)(2)),帮我找出了不少C++中的内存管理错误,前一阵子还在纠结为什么VS 2013下运行良好的程序到了Lin ...
- OneZero第七周第一次站立会议(2016.5.9)
1. 时间: 12:15--12:25 共计10分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...
- Python中print/format字符串格式化实例
Python 字符串格式化使用 "字符 %格式1 %格式2 字符"%(变量1,变量2),%格式表示接受变量的类型.简单的使用例子如下 # 例:字符串格式化Name = '17jo' ...
- C# PictureBox控件畫圖
PictureBox的正方向: BitMap初始化: Bitmap bt = new Bitmap(Width,Height); Graphics gdi = Graphics.FromIm ...
- Square Root
Square RootWhen the square root functional configuration is selected, a simplified CORDIC algorithm ...