[LeetCode] Merge Intervals
来源:互联网 发布:php pre match 编辑:程序博客网 时间:2024/05/16 07:10
/** * 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 compare(const Interval &inv1, const Interval &inv2) { return inv1.start < inv2.start;}class Solution {public: vector<Interval> merge(vector<Interval> &intervals) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<Interval> ret; if (intervals.empty()) return ret; sort(intervals.begin(), intervals.end(), compare); ret.push_back(intervals[0]); for (int i = 1; i < intervals.size(); i++) { if (ret.back().end >= intervals[i].start) ret.back().end = max(ret.back().end, intervals[i].end); else ret.push_back(intervals[i]); } return ret; }};
Small Case: 4ms
Large Case: 68ms
Time: O(n)
Space: O(n)
- LeetCode: Merge Intervals
- LeetCode Merge Intervals
- LeetCode: Merge Intervals
- [Leetcode] Merge Intervals
- leetcode 28: Merge Intervals
- Leetcode: Merge Intervals
- [LeetCode] Merge Intervals
- [LeetCode]Merge Intervals
- leetcode merge intervals
- [leetcode]Merge Intervals
- LeetCode-Merge Intervals
- [leetcode] Merge Intervals
- LeetCode - Merge Intervals
- 【leetcode】Merge Intervals
- leetcode Merge Intervals
- LeetCode Merge Intervals
- [LeetCode] Merge Intervals
- LeetCode | Merge Intervals
- 从一道笔试题来看指针的使用
- 王爽《汇编语言》学习笔记--第一章
- 移动平台广告类型
- i.mx536(cotex-a8核)的SPI驱动理解一(probe)
- WIN SDK 添加Tab控件 List Control with CheckBox
- [LeetCode] Merge Intervals
- 图解Javascript核心内容
- 杭电OJ——1024 Max Sum Plus Plus 详细分析+优化全过程
- 不喜欢吃甜食,所以我更喜欢强扭的瓜
- 每日构建与持续集成联系与区别
- 使用Word2003进行公式排版
- 用数字解释软件开发的8个为什么
- HDU 2476 字符串a转化为字符串b需要几次操作 结合编辑距离
- 关于数据库bak文件的使用问题解决