Beat 100% simple c++ solution


  • 0
    K
    class compare{
    public:
    bool operator()(Interval a, Interval b){
        return a.start < b.start;
    }
    };
    class Solution {
    public:
    vector<Interval> merge(vector<Interval>& intervals) {
        vector<Interval>res;
        sort(intervals.begin(), intervals.end(), compare());
        for(auto & set : intervals){
            if(res.empty())res.push_back(set);
            else {
                if(set.start <= res.back().end){
                    res.back().end=max(set.end, res.back().end);
                    continue;
                }
                else res.push_back(set);
            }
        }
        return res;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.