刚开始做这个题的时候绕了好大的圈,对问题的分析不全面,没能考虑所有情况,做的很纠结。后来看了下大神的做法很受启发,改了改代码,最终提交了。

public static ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
ArrayList<Interval> ips=null;
ips=new ArrayList<Interval>();
int p=0;
int q=intervals.size()-1;
if(intervals.size()==0){
ips.add(newInterval);
return ips;
}else{
if(intervals.get(p).start>newInterval.end){
ips.add(newInterval);
ips.addAll(intervals);
}else if(intervals.get(q).end<newInterval.start){
ips.addAll(intervals);
ips.add(newInterval);
}else {
while(p<=q){
if(intervals.get(q).start>newInterval.end){
q--;
}else if(intervals.get(p).end<newInterval.start){
ips.add(intervals.get(p));
p++;
}else {
break;
} }
if(p<=q){
ips.add(new Interval(
intervals.get(p).start<newInterval.start?intervals.get(p).start:newInterval.start,
intervals.get(q).end>newInterval.end?intervals.get(q).end:newInterval.end
));
q++;
}else{
ips.add(newInterval);
q++;
} for(;q<intervals.size();q++){
ips.add(intervals.get(q));
}
} return ips;
}
}

Insert Interval 面试题leetcode.的更多相关文章

  1. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  2. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  3. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  4. 【LeetCode】57. Insert Interval [Interval 系列]

    LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间;  3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...

  5. 【leetcode】Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  6. 【LeetCode】57. Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  7. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

  8. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  9. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

随机推荐

  1. nyoj 36 最长公共子序列【LCS模板】

    最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...

  2. C# 光标文件的创建

    base.m_cursor = new System.Windows.Forms.Cursor(GetType(), "Resources.MeasuredisTool.cur") ...

  3. CMDLINE的解析

    在linux的config文件中有一个特殊的宏定义CMDLINE,以前也一直在使用这个宏的参数,但是真正这个宏的解析和使用却不怎么明确.这次有机会多对它有些了解,不妨把这个浅显的认识说出来,记下来. ...

  4. 【课程分享】Oracle数据库系统project师

    对这个课程有兴趣的朋友能够加我的QQ2059055336和我联系 一.本课程是怎么样的一门课程 1.1.课程的背景 该课程是Oracle数据库系统project师培训课程 Oracle Databas ...

  5. The Six Types of Rails Association

    翻译整理自:http://guides.rubyonrails.org/v3.2.13/association_basics.html 想吐槽一句,http://guides.ruby-china.o ...

  6. Javascript --扩展String实现替换字符串中index处字符

    String.prototype.replaceCharAt = function(n,c){ return this.substr(0, n)+ c + this.substr(n+1,this.l ...

  7. iOS RSA 加密解密及签名验证

    1.首先要下载openssl.这个不用说,直接官网下载或者用brew install openssl下载. 2.终端生成私钥密钥. 2.1生成私钥 openssl genrsa - 2.2生成密钥 o ...

  8. javascript 高级程序设计(二)-在html中使用javascript

    <script> async 可选 charset 可选 defer 可选 language 已废弃 src 可选 type 可选

  9. codevs 2822爱在心中

    不想吐槽题目.... /* K bulabula 算法(好像用哪个T bulabula更简单 然而我并不会 - -) 丑陋的处理cnt: Printf时 cnt中 ans[i][0]==1 的删掉 然 ...

  10. 详细分析Orchard的Content、Drivers, Shapes and Placement 类型

    本文原文来自:http://skywalkersoftwaredevelopment.net/blog/a-closer-look-at-content-types-drivers-shapes-an ...