力扣第56题 合并区间

力扣第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;
    }
};
上一篇:Leetcode练习之贪心思想


下一篇:Python间隔interesction