其他分享
首页 > 其他分享> > 力扣第56题 合并区间

力扣第56题 合并区间

作者:互联网

力扣第56题 合并区间

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) 
    {
        sort(intervals.begin(), intervals.end(), [](const vector<int> &pl1, const vector<int> &pl2) {
            return pl1[0] < pl2[0];
        });
        int cur = 0, len = intervals.size();
        for (int i = 1; i < len; i++)
        {
            if (intervals[cur][1] < intervals[i][0])
            {
                cur++;
                intervals[cur][0] = intervals[i][0];
            }
            else if(intervals[cur][1] >= intervals[i][1])
                continue;
            intervals[cur][1] = intervals[i][1];
        }
        auto itor = intervals.begin() + cur;
        if(itor != intervals.end())
            intervals.erase(itor + 1, intervals.end());
        return intervals;
    }
};

标签:itor,end,cur,56,合并,intervals,力扣,vector,pl2
来源: https://www.cnblogs.com/woodjay/p/12716885.html