leetcode-57.插入区间

题目

在这里插入图片描述leetcode的每日一题,看到是hard有些慌,但是看题目似乎并不难; 有两种想法,在本地空间对intervals进行修改,或新建vector来插入
本地修改的方法需要做很多判断,而且需要在本地vector进行多次erase操作,效率并不会比新建vector效率高!

本地修改代码(要是遇到很多需要合并的单元会导致很多erase操作,很耗时!):

class Solution {
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        int count = 0;
        if(intervals.size()>0){
            if((intervals.front())[0]>newInterval[0] && (intervals.back())[1]<newInterval[1]){
                // 用于应对测试用例中最后一个超长输入
                // 这也是直接在intervals上操作的坏处,要是intervals有很多需要合并的单元,那么需要很多次erase,容易导致超时!
                vector<vector<int>> res;
                res.push_back(newInterval);
                return res;
            }
        }
        for(vector<vector<int>>::iterator iter=intervals.begin();iter!=intervals.end();){
            if(newInterval[1]<(*iter)[0]){
                // newInterval在左侧
                if(count == 0){
                    intervals.insert(iter,newInterval);
                    count ++;
                }
                return intervals;
            }
            else{
                if(newInterval[0]>(*iter)[1]){
                // newInterval在右侧
                    iter++;
                    continue;
                }
                else{
                    //  有交集
                    int left = min((*iter)[0],newInterval[0]);
                    int right = max((*iter)[1],newInterval[1]);
                    intervals.erase(iter);
                    newInterval = {left,right};
                }
            }
        }
        if(count == 0)
            intervals.push_back(newInterval);
        return intervals;
    }
};

新建vector:

class Solution {
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        vector<vector<int>> res;
        int count = 0;
        for(int i=0;i<intervals.size();i++){
            if(newInterval[1]<intervals[i][0]){
                // newInterval在左侧,且无交集
                if(count == 0){
                    res.push_back(newInterval);
                    count++;
                }
                res.push_back(intervals[i]);
            }
            else{
                if(newInterval[0]>intervals[i][1]){
                    // newInterval在右侧,且无交集
                    res.push_back(intervals[i]);
                }
                else{
                    // 有交集
                    int left = min(newInterval[0],intervals[i][0]);
                    int right = max(newInterval[1],intervals[i][1]);
                    newInterval = {left,right};
                }
            }
        }
        if(count == 0)
            res.push_back(newInterval);
        return res;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值