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].


题解:利用变量insertPos记录最终区间插入的位置。遍历intervals,那么当前遍历的interval和newInterval有以下几种情况:

代码如下:

 /**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> answer = new ArrayList<Interval>();
int insertPos = 0;
for(Interval in:intervals){
if(newInterval.start > in.end){
answer.add(in);
insertPos++;
}
else if(in.start > newInterval.end)
{
answer.add(in);
}
else{
newInterval.start = Math.min(in.start, newInterval.start);
newInterval.end = Math.max(in.end, newInterval.end);
}
}
answer.add(insertPos, newInterval);
return answer;
}
}

【leetcode刷题笔记】Insert Interval的更多相关文章

  1. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  2. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  3. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  4. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  5. LeetCode刷题笔记(1-9)

    LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...

  6. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...

  7. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

  8. LeetCode刷题笔记(1)常用知识点

    1.Integer.parseInt(String s, int radix)方法的作用是:将radix进制的字符串s转化成10进制的int型数字并返回. Integer.valueof(String ...

  9. LeetCode刷题笔记-回溯法-分割回文串

    题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa", ...

随机推荐

  1. 开源码应用之Eclipse篇

    开写这篇的时候,恰逢Eclpse Mars(4.5)正式公布,最终由日蚀变登火星了,也离我開始基于Eclipse开发产品已经过去10年,这10年间,经历了Eclipse由私有核心框架到拥抱OSGi, ...

  2. request.getParameterValues()用法

    <form name="checkform" method="post" action="getvalue.jsp"> 你希望学 ...

  3. java开发目前技术选型

    目前系统采用 1.后端 服务框架:Dubbo.zookeeper 缓存:Redis.ehcache 消息中间件:ActiveMQ,kafka 负载均衡:Nginx 分布式文件:FastDFS 数据库连 ...

  4. json responseJson

    private void doResoponseJson(HttpServletResponse resp,String jsonString){ Trace.logError(Trace.COMPO ...

  5. -webkit-transition: all .2s ease-in-out;

    W3C标准中对CSS3的transition这是样描述的:CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发,并 ...

  6. 在MathType中输入罗马数字的方法

    MathType作为数学公式编辑器的编辑功能非常的强大,其中包含了许许多多各种各样的数学符号,甚至标记符号也很全面.编辑公式时有时为了让公式看起来会更有条理,会进行一定的序号设置,当然也可以对公式进行 ...

  7. python导入模块报错:ImportError: No module named mysql.connector(安装 mysql)

    python的版本是 $ python --version Python 2.7.12 报错代码如下 import mysql.connector 报错信息是 ImportError: No modu ...

  8. yii 国际化

    http://www.yiichina.com/doc/guide/2.0/tutorial-i18n config/main.php 外层加 'language' => 'en-US', 's ...

  9. POJ1365 Prime Land【质因数分解】【素数】【水题】

    题目链接: http://poj.org/problem?id=1365 题目大意: 告诉你一个数的质因数x的全部底数pi和幂ei.输出x-1的质因数的全部底数和幂 解题思路: 这道题不难.可是题意特 ...

  10. VC编译那些事儿

    转载自:http://blog.csdn.net/wowolook/article/details/8077153     最近又被ms的编译选译纠结了一下,运行程序是老是弹出0x14b1 or 71 ...