57. 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.
Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].
Subscribe to see which companies asked this question.

这是要实现区间的并集,思路是,遍历已经存在的区间,当新区间end<遍历元素的start,遍历结束,其余的有如下几种情况:1,新区间的start在当前的区间之间,但是end在当前元素后面,则将新区间的start=当前元素的start;2,新区间的end在当前区间内,但是start在区间前面,则将新区间end=当前区间的end;3,当前区间包括新区间,则令新区间=当前区间;4,新区间包括当前区间,则进行下一个区间的比较。当跳出循环之后,将新区间放入list中,还有原list中没有比较的区间。具体代码如下:

public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
        ArrayList<Interval>newIntervals=new ArrayList<>();
        int i=0;
        for (; i < intervals.size(); i++) {
            Interval interval=intervals.get(i);
            if (interval.start>newInterval.end) break;
            if (newInterval.start>=interval.start&&newInterval.start<=interval.end&&newInterval.end>interval.end) 
                newInterval.start=interval.start;
            if (newInterval.start<interval.start&&newInterval.end>=interval.start&&newInterval.end<=interval.end)
                newInterval.end=interval.end;
            if (newInterval.start>=interval.start&&newInterval.end<=interval.end){
                newInterval.start=interval.start;
                newInterval.end=interval.end;
            }
            if (newInterval.start<=interval.start&&newInterval.end>=interval.end) continue;
            if (newInterval.start>interval.end) newIntervals.add(interval);         
        }
        newIntervals.add(newInterval);
        for (; i < intervals.size(); i++) {
            newIntervals.add(intervals.get(i));
        }       
        return newIntervals;
    }
  • 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、付费专栏及课程。

余额充值