Leetcode中的intervals

题目一:Merge Intervals

Given a collection of intervals, merge all overlapping intervals

思路:这道题题意和思路都很明确,但是又很多tricks。

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:
    //先按照start排序,然后按照end排序
	static bool comp(const Interval& i1,const Interval& i2){
        if(i1.start!=i2.start)
            return i1.start<i2.start;
        else 
            return i1.end<i2.end;
    }
    
    vector<Interval> merge(vector<Interval> &intervals) {
        if(intervals.size()==0  || intervals.size()==1) return intervals;
        //先排序
        sort(intervals.begin(), intervals.end(), comp);
        
        vector<Interval> new_intervals;
        int start, end;
        start=intervals[0].start;  //维护一个开始点和结束点
        end=intervals[0].end;
        
        for(int i=1;i<intervals.size();i++){
            //如果下一个与当前这段没有关联
            if(intervals[i].start>end){
                new_intervals.push_back(Interval(start,end));
                start=intervals[i].start;    
                end=intervals[i].end;
            }
            else{                   //如果有关联,更新end
                if(intervals[i].end>end)
                    end=intervals[i].end;
            } 
        }
        new_intervals.push_back(Interval(start,end));
        return new_intervals;
    }   
};

题目二: Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times.

思路:这道题比上一道题稍显复杂,关键是要找到合适的插入点。插入之后,可能涉及到merge操作,也可能仅仅是insert

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        vector<Interval> res;
        int start,end;
        int insert=0;
        
        for(int i=0;i<intervals.size();i++){
            //找到第一个与newInterval相关的interval
            if(newInterval.start<=intervals[i].end){   //有重叠和完全不重叠两种情况
                start=min(intervals[i].start,newInterval.start);
                if(newInterval.end<intervals[i].start){
                    res.push_back(newInterval);
                }
                else{                                  //合并重叠的部分
                    end=newInterval.end;
                    //循环条件是能够到着下一个interval
                    while(i<intervals.size() && end>=intervals[i].start){ 
                        end=max(intervals[i].end, end);
                        i++;
                    }
                    res.push_back(Interval(start,end));
                } 
                //处理剩下部分
                insert=1;
                while(i<intervals.size()) 
                    res.push_back(intervals[i++]);
            }
            else res.push_back(intervals[i]);
        }
        if(insert==0) res.push_back(newInterval);
        
        return res;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值