57. Insert Interval

查看题目

一种很简洁的方法,只需要三个while循环,时间复杂度为O(n)

第一个while循环负责在n新区间左部的,第二个while负责和新区间重合的,第三个while循环负责在新区间右部的。

/**
 * 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> rtn;
        
        auto it = intervals.begin();
        while (it != intervals.end() && it->end < newInterval.start) rtn.push_back(*(it++));
        
        while (it != intervals.end() && it->start <= newInterval.end) {
            if (it->start < newInterval.start) newInterval.start = it->start;
            if (it->end > newInterval.end) newInterval.end = it->end;
            it ++;
        }
        
        rtn.push_back(newInterval);
        
        while (it != intervals.end()) rtn.push_back(*(it++));
        
        return rtn;
    }
};

另外,可以通过二分查找的办法寻找和新区间重合的区间的序号。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]和\[2\]中提到的错误是由于使用PyInstaller打包时出现的问题。这些错误可能是由于PyInstaller无法正确处理某些特定的Python模块或库导致的。解决这些错误的方法可能是更新PyInstaller版本,或者尝试使用其他打包工具。 至于引用\[3\]中提到的错误,即"'pandas._libs.interval.Interval' object has no attribute 'total_iv'",这个错误是由于在代码中使用了pandas._libs.interval.Interval对象的total_iv属性,但该属性在该对象中并不存在。这可能是由于使用了错误的属性名称或者版本不兼容导致的。要解决这个错误,您可以检查代码中对该属性的使用,并确保使用正确的属性名称。如果您使用的是较旧的pandas版本,可能需要升级到较新的版本以获得所需的属性。 总结起来,要解决这些错误,您可以尝试以下几个步骤: 1. 更新PyInstaller版本或尝试其他打包工具。 2. 检查代码中对属性的使用,并确保使用正确的属性名称。 3. 如果使用的是较旧的pandas版本,考虑升级到较新的版本以获得所需的属性。 希望这些解决方案能帮助您解决问题! #### 引用[.reference_title] - *1* *2* [成功解决pyinstaller打包AttributeError:type object pandas._TSObject has no attribute _reduce_cython_](https://blog.csdn.net/qq_41185868/article/details/80601983)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [ModuleNotFoundError: No module named ‘pandas._libs’](https://blog.csdn.net/yangning7777777/article/details/120909561)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值