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 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
随机推荐
- redis中各种数据类型对应的jedis操作命令
redis中各种数据类型对应的jedis操作命令 一.常用数据类型简介: redis常用五种数据类型:string,hash,list,set,zset(sorted set). 1.String类型 ...
- SSH服务及花生壳域名解析
一.安装说明以CentOS 5为例 1.安装必要的开发包 [root@localhost ~]# yum install gcc gcc-c++ autoconf automake 2.下载phddn ...
- UltraISO对光盘镜像的常用操作
UltraISO,它能直接编辑光盘映像或者从光盘映像文件里面提取文件:可以从CD-ROM里面制作光盘映像:也可以把硬盘上的文件制作成ISO文件:可以把ISO中启动信息保存下来,也可以为ISO添加启动功 ...
- Google Android开发入门与实战
http://www.pc6.com/softview/SoftView_58350.html
- etrace 跟踪程序函数动态执行流程
https://github.com/elcritch/etrace 窗口1: 监控窗口,执行监控程序,显示监控结果 [root@monitor example]# pwd /root/etrace- ...
- Enable Access Logs in JBoss 7 and tomcat--转
JBoss 7 is slightly different than earlier version JBoss 5 or 6. The procedure to enable access logs ...
- spring mvc DispatcherServlet详解之三---request通过ModelAndView中获取View实例的过程
整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet第二步:通过request从Controller获取ModelAndView.现在来讲解第三步:reques ...
- spring mvc DispatcherServlet详解之四---视图渲染过程
整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的最后一步:视图渲染.视图渲染的过程是在获取到ModelAndView后的过程 ...
- linux共享windows资料
linux 只有NTFS格式不能访问,其的都可以.1.用fdisk -l 查看分区表.2.然后用mount -t vfat /mnt/hda1 /dev/hda1 就可以了./mnt/hda1是一普通 ...
- 浏览器中JavaScript执行原理
本章我们讨论javascript在浏览器中是如果工作的,包括:下载.解析.执行的全过程.javascript的这些讨人嫌的地方我们是知道的: i.需要串行下载 ii.需要解析 iii.需要串行执行 而 ...