leetcode 56区间合并

 

class Solution {
public:
    static bool cmp(vector<int> a,vector<int> b){
        return a[0]<b[0];
    }    
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> res;
        
        sort(intervals.begin(),intervals.end(),cmp);
        for(int i=0;i<intervals.size();i++){
            int x=intervals[i][0];int y=intervals[i][1];
            while(i+1<intervals.size()&&y>=intervals[i+1][0]){
                i++;
                y=max(intervals[i][1],y);
            }
            res.push_back({x,y});
        }
        return res;
    }
};

 

posted @ 2019-05-27 22:26  Joel_Wang  阅读(182)  评论(0编辑  收藏  举报