## 小do在努力

Talk is cheap, show me the code ~ turn potential to reality---Do

# 【leetcode】56. Merge Intervals

@requires_authorization
@create_time 2015.7.10 11:07
@author johnsondu
@url https://leetcode.com/problems/merge-intervals/
/**
*  进行数组排序，然后按照贪心策略进行合并，具体是考察
*  下一个元素的start是否是介于当前的start和end之间，
*  如果是，则根据需要更新当前end。
*  时间复杂度(O(nlogn))-->排序， 结果获取O(n)
*  空间复杂度(O(n))
*/

/**
* 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 cmp(const Interval &a, const Interval &b){
if(a.start == b.start) return a.end < b.end;
return a.start < b.start;
}

vector<Interval> merge(vector<Interval>& intervals) {
int len = intervals.size();
sort(intervals.begin(), intervals.end(), cmp);

vector<Interval> ans;
for(int i = 0; i < len; i ++){
int st = intervals[i].start;
int ed = intervals[i].end;
int idx = i + 1;
while(idx < len){
if(intervals[idx].start >= st && intervals[idx].start <= ed){
if(intervals[idx].end > ed) ed = intervals[idx].end;
idx ++;
}
else break;
}
Interval tmp(st, ed);
ans.push_back(tmp);
i = idx - 1;
}

return ans;
}
};

#### LeetCode 56:Merge Intervals

2015-06-02 21:17:41

#### leetCode练习（56）

2016-10-10 19:44:30

#### LeetCode（56）Merge Intervals

2015-09-14 19:13:30

#### LeetCode 56

2016-05-30 11:11:28

#### Leetcode #56. Merge Intervals 间隔合并 解题报告

2016-04-26 23:54:53

#### 56. Merge Intervals-leetcode-java

2016-05-06 18:48:42

#### LeetCode --- 56. Merge Intervals

2015-03-14 23:03:47

#### 个人记录-LeetCode 56. Merge Intervals

2016-12-27 20:19:42

#### Leetcode 56 Merge Intervals

2016-09-18 20:21:59

#### <LeetCode>56. 合并区间

2018-05-16 12:17:10