标签:
感觉有一点进步了,但是思路还是不够犀利。
/** * 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: vector<Interval> merge(vector<Interval>& intervals) { vector<pair<int,int>> r; for(int i=0;i<intervals.size();i++) r.push_back(make_pair(intervals[i].start,intervals[i].end)); sort(r.begin(),r.end()); int index=0; vector<Interval> res; while(index<r.size()){ int start=r[index].first; int end=r[index].second; if(start == INT_MAX) { index++; continue; } for(int i=index+1;i<r.size();i++) { if(r[i].first == end){ end=r[i].second; r[i].first=INT_MAX,r[i].second=INT_MAX; } } while(r[index+1].first >= start && r[index+1].second<=end && r[index+1].second>=start&&r[index+1].second<=end) { index++; } while(end>=r[index+1].first && end<=r[index+1].second && index+1 <=r.size()) { index++; end=r[index].second; } res.push_back(Interval(start,end)); index++; } return res; } };
LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。
标签:
原文地址:http://www.cnblogs.com/yanqi110/p/5013454.html