Insert Interval 面试题leetcode.
刚开始做这个题的时候绕了好大的圈,对问题的分析不全面,没能考虑所有情况,做的很纠结。后来看了下大神的做法很受启发,改了改代码,最终提交了。
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.的更多相关文章
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- 【leetcode】Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【LeetCode】57. Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- Leetcode: Merge/Insert Interval
题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- 第一周 Leetcode 57. Insert Interval (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
随机推荐
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- llvm學習(二)————llvm編譯與環境構建
本文由博主原创,转载请注明出处(保留此处和链接): IT人生(http://blog.csdn.net/robinblog/article/details/17339027) 在2011十月份的时候, ...
- css开发经验&错误习惯
CSS开发经验 1.尽量用class来定义样式.尽量少使用 .div1 ul li{}这样的样式下去,因为如果li里面还有<div><ul><li>这些元素的话会 ...
- [Javascript] Creating an Immutable Object Graph with Immutable.js Map()
Learn how to create an Immutable.Map() through plain Javascript object construction and also via arr ...
- [Angular 2] 9. Replace ng-modle with #ref & events
Let's say you want to write a simple data bing app. when you type in a text box, somewhere in the ap ...
- UVALive3713-Astronauts(2-SAT)
题目链接 题意:有A.B.C3个任务分配给n个宇航员,当中每一个宇航员恰好分配一个任务.如果n个宇航员的平均年龄为x,仅仅有年龄大于x的才干领取A任务:仅仅有年龄严格小于x的才干领取B任务,而任务C没 ...
- systemTAP 学习
http://blog.csdn.net/moonvs2010/article/category/1570309
- Java的演变过程
1. 1996.01.23 JDK1.0 代号Oak:212个类.8个包: 2. 1997.02.19 JDK1.1 504个类.23个包: Java Bean.远程方法调用(RMI).JAR文件格式 ...
- wpf提示背景,资源样式
查找资源时多用element.TryFindResource() <TextBox FontSize="17" Height="26" Margin=&q ...
- 【转载】Shared Configuration
Introduction The Internet changes the ways in which companies handle their day-to-day business and h ...