标签:style blog color io for ar art 问题
[问题描述]
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18]
,
return [1,6],[8,10],[15,18]
.
[解题思路]
排序 + 比较
1 bool cmp_Interval(Interval a, Interval b) 2 { 3 return a.start == b.start?(a.end < b.end):(a.start < b.start); 4 } 5 class Solution { 6 public: 7 vector<Interval> merge(vector<Interval> &intervals) { 8 std::vector<Interval> ans; 9 if (intervals.size() == 0) 10 return ans; 11 std::sort(intervals.begin(), intervals.end(), cmp_Interval); 12 ans.push_back(intervals[0]); 13 for (int i = 0; i < intervals.size(); i ++){ 14 if (ans[ans.size() - 1].end >= intervals[i].start) 15 ans[ans.size() - 1].end = std::max(ans[ans.size() - 1].end, intervals[i].end); 16 else 17 ans.push_back(intervals[i] ); 18 } 19 return ans; 20 } 21 };
leetcode -- Merge Intervals,布布扣,bubuko.com
标签:style blog color io for ar art 问题
原文地址:http://www.cnblogs.com/taizy/p/3915599.html