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 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
随机推荐
- Centroid - SGU 134(树的搜索)
题目大意:给你一个树,树每个点都有一个值, 这个点的的值就等于所有儿子里面点最多的那个儿子,值最小的就叫做重心,求出重心,还有所有等于重心的点,按照升序输出. 分析:就是一个简单的搜索树,求出来最大的 ...
- hdoj 2524 矩形A + B【递推】
矩形A + B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- C# ToString格式大全
C# 货币 2.5.ToString("C"); // ¥2.50 // D 10进制数 25.ToString("D5"); // 25000 // E ...
- redux-applyMiddleware实现理解+自定义中间件
前言: 终于好好理解了middleware.... 1.redux middleware提供的是位于 action 被发起之后,到达 reducer 之前的扩展点. redux通过store.disp ...
- Linux下安装 TestLink常见问题解决方法
Read/write permissions For security reason we suggest that directories tagged with [S] on following ...
- Codeforces Beta Round #51 D. Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- python s12 day3
python s12 day3 深浅拷贝 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- oracle中创建表时添加注释
CREATE TABLE t1(id varchar2(32) primary key,name VARCHAR2(32) ,age VARCHAR2(32) );comment on column ...
- bootstrap3学习1:响应式布局layout
在去年的这个时候写过关于bootstrap的相关文章(见:bootstrap2学习1:基本CSS样式),然后就搁置了,原因是因为当时对bootstrap的了解不深,并且当时v2版本对响应式设计的不是非 ...