美文网首页
insert-interval

insert-interval

作者: DaiMorph | 来源:发表于2019-07-23 23:14 被阅读0次
    class Solution {
    public:
        vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
            vector<Interval>res;
            int i=0;
            for(;i<intervals.size();i++)
            {
                if(newInterval.start>intervals[i].end)
                    res.push_back(intervals[i]);
                else if(newInterval.end<intervals[i].start)
                {
                    
                    break;
                }
                else{
                    newInterval.start=min(newInterval.start,intervals[i].start);
                    newInterval.end=max(newInterval.end,intervals[i].end);
                }
            }
            res.push_back(newInterval);
            for(;i<intervals.size();i++)res.push_back(intervals[i]);
            return res;
        }
    };
    

    相关文章

      网友评论

          本文标题:insert-interval

          本文链接:https://www.haomeiwen.com/subject/xjvzlctx.html