16ms simple s++ solution


  • 0
    Y
    class Solution {
    public:
        vector<Interval> merge(vector<Interval>& intervals) {
            std::sort(intervals.begin(),intervals.end(),compare());
            vector<Interval> reval;
            vector<Interval>::const_iterator iter = intervals.begin();
            while(iter<intervals.end()){
                if(reval.empty() || reval[reval.size()-1].end<iter->start){
                    reval.push_back(*iter);
                } else {
                    reval[reval.size()-1].end = std::max(reval[reval.size()-1].end,iter->end);
                }
                ++ iter;
            }
            return reval;
        }
    private:
        struct compare{
        public :
            bool operator()(const Interval & left, const Interval & right) const{
                return left.start < right.start;
            }    
        };
    };
    

Log in to reply
 

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