码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Merge Intervals

时间:2015-03-21 07:32:56      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

 1 /**
 2  * Definition for an interval.
 3  * struct Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() : start(0), end(0) {}
 7  *     Interval(int s, int e) : start(s), end(e) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<Interval> merge(vector<Interval> &intervals) {
13         if (intervals.size() < 2) return intervals;
14         int len = intervals.size(), index = 0;
15         vector<Interval> result;
16         vector<int> left, right;
17         for (Interval interval : intervals) {
18             left.push_back(interval.start);
19             right.push_back(interval.end);
20         }
21         sort(left.begin(), left.end());
22         sort(right.begin(), right.end());
23         while (index < len) {
24             int s = left[index], e = right[index++];
25             while (index < len) {
26                 if (e >= left[index]) {
27                     e = right[index++];
28                 } else break;
29             }
30             result.push_back(Interval(s, e));
31         }
32         return result;
33     }
34 };

 

Also you can check this solution: http://fisherlei.blogspot.com/2013/04/leetcode-merge-intervals-solution.html

LeetCode – Refresh – Merge Intervals

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4355085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!