标签:
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