C++ 16ms solution


  • 0
    H
    class Solution {
    public:
        vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
            vector<Interval> ans;
            auto iter = intervals.begin();
            for(; iter != intervals.end(); iter ++) {
                if(newInterval.end < (*iter).start) break;
                else if(newInterval.start > (*iter).end) ans.push_back(*iter);
                else {
                    newInterval.start = min(newInterval.start, (*iter).start);
                    newInterval.end = max(newInterval.end, (*iter).end);
                }
            }
            ans.push_back(newInterval);
            for(; iter != intervals.end(); iter ++) ans.push_back(*iter);
            return ans;
        }
    };
    

Log in to reply
 

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