57. Insert Interval
来源:互联网 发布:淘宝店运营商靠谱吗 编辑:程序博客网 时间:2024/05/17 23:55
/** * 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: vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) { auto i = upper_bound(intervals.begin(), intervals.end(), newInterval, comp1); intervals.insert(i, newInterval); return merge(intervals); } static bool comp1(const Interval &a, const Interval &b){ return a.start < b.start; } static bool comp(const Interval &a, const Interval &b){ return a.start < b.start; } vector<Interval> merge(vector<Interval>& intervals) { int pos = 0, sz = intervals.size(); intervals.push_back(Interval(0x7fffffff, 0x7fffffff)); vector<Interval> ans; while(pos < sz){ int maxRight = intervals[pos].end, t = pos; while(pos < sz && maxRight >= intervals[++pos].start) { maxRight = max(maxRight, intervals[pos].end); } ans.push_back(Interval(intervals[t].start, maxRight)); } return ans; }};
阅读全文
0 0
- LeetCode --- 57. Insert Interval
- [Leetcode] 57. Insert Interval
- [LeetCode]57.Insert Interval
- [leetcode] 57.Insert Interval
- 57. Insert Interval
- [leetcode] 57.Insert Interval
- 57. Insert Interval
- 57. Insert Interval
- lkeetcode 57. Insert Interval
- 57. Insert Interval
- Leetcode 57. Insert Interval
- LeetCode 57. Insert Interval
- 57. Insert Interval
- 57. Insert Interval
- 57. Insert Interval
- 57. Insert Interval
- LeetCode 57. Insert Interval
- [LeetCode] 57. Insert Interval
- P2161 [SHOI2009]Booking 会场预约
- HPUX常用核心参数配置说明
- Unix socket进程间通信
- 翻译:AVPlayerItemVideoOutput
- 读Zepto源码之Callbacks模块
- 57. Insert Interval
- 【poj 1679】The Unique MST 【次小生成树 模板】
- 明远智睿i.MX6开发板EK200捡漏之二:RTC时间修改
- Win10 Microsoft IME是什么进程?占用CPU高如何解决?
- (3)基础学习:渲染
- JAVA面试积累1
- HDU 2255 KM算法—模板
- Linux基础
- 学习虫师的《web接口开发与自动化测试》第六章