1. 原题链接

https://leetcode.com/problems/insert-interval/description/

2. 题目要求

该题与上一题的区别在于,插入一个新的interval对象,将此对象与所给列表里的Interval对象进行合并。

3. 解题思路

首先遍历Interval对象列表,找到插入位置。将插入位置之前的列表中的Interval对象直接加入结果列表。

然后查找所要插入Interval对象的end属性在列表中的位置。

最后将列表中剩余的Interval对象全部加入结果列表。

4. 代码实现

import java.util.LinkedList;
import java.util.List; public class InsertIntervals57 { public static void main(String[] args) { Interval in1 = new Interval(1, 2);
Interval in2 = new Interval(3, 5);
Interval in3 = new Interval(6, 7);
Interval in4 = new Interval(8, 10);
Interval in5 = new Interval(12, 16);
Interval in = new Interval(4, 9);
List<Interval> ls = new LinkedList<Interval>();
ls.add(in1);
ls.add(in2);
ls.add(in3);
ls.add(in4);
ls.add(in5);
for (Interval i : insert(ls, in))
System.out.println(i.start + " " + i.end); } public static List<Interval> insert(List<Interval> intervals, Interval interval) { List<Interval> res = new LinkedList<Interval>();
if (intervals.size() == 0) {
res.add(interval);
return res;
}
int i = 0; // 将插入位置之前的元素直接加入结果列表中
while (i < intervals.size() && intervals.get(i).end < interval.start) {
res.add(intervals.get(i++));
} // 将所要插入的元素与李彪中的对象进行合并
while (i < intervals.size() && intervals.get(i).start <= interval.end) {
interval = new Interval( // we could mutate newInterval here also
Math.min(interval.start, intervals.get(i).start),
Math.max(interval.end, intervals.get(i).end));
i++;
}
res.add(interval); // 将剩余的所有元素插入结果列表
while (i < intervals.size())
res.add(intervals.get(i++));
return res;
}
}

  

LeetCode: 57. Insert Interval(Hard)的更多相关文章

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

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

  2. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

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

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

  4. [LeetCode] 57. Insert Interval 插入区间

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

  5. LeetCode 57. Insert Interval 插入区间 (C++/Java)

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  6. Leetcode#57 Insert Interval

    原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...

  7. [LeetCode] 57. Insert Interval 解决思路

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

  8. [leetcode]57. Insert Interval插入区间

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

  9. Leetcode 57: Insert Interval 让代码更好读, 更容易测试.

    阅读了几个博客, 决定写一个简易版本; 忙着做更多题, 没有时间多考虑优化代码, 所以, 就写一个试试运气. http://blog.csdn.net/kenden23/article/details ...

随机推荐

  1. Android webview 点击超链接打开新的webview

    webview.setWebViewClient(new webViewClient() { HitTestResult hit = view.getHitTestResult(); if (hit ...

  2. hdu 6169 gems gems gems【DP】

    题目链接:hdu 6169 gems gems gems Now there are n gems, each of which has its own value. Alice and Bob pl ...

  3. 页面间传递前端请求参数和获取参数:Model model,HttpServletRequest request, ModelMap map参数使用与区别

    Model model, HttpServletRequest request, ModelMap map声明变量 一.下面的方法是需要将请求发过来的数据(或者说参数)传递到重定向的页面/转发的页面的 ...

  4. Codeforces Round #527 (Div. 3) D2. Great Vova Wall (Version 2) 【思维】

    传送门:http://codeforces.com/contest/1092/problem/D2 D2. Great Vova Wall (Version 2) time limit per tes ...

  5. ServletRequest的四个主要方法

    package com.yunqing.servlet; import javax.servlet.*; import java.io.IOException; import java.util.Ar ...

  6. 利用python 传输文件

    最近在学python3 发现了一个很有用的功能,该功能可以将安装python 的机器作为一台http 服务器来分享本机的文件, 具体的使用记录如下 python3 的使用方法 直接在windows 的 ...

  7. JAVA格式化解析日期

  8. iOS之利用runtime,避免可变数组和可变字典为nil或者数组越界导致的崩溃

    NSArray.NSMutableArray.NSDictionary.NSMutableDictionary.是我们的在iOS开发中非常常用的类.当然,在享受这些类的便利的同时,它们也给我们带来一些 ...

  9. stl学习之namespace

    一.为什么需要命名空间(问题提出) 命名空间是ANSIC++引入的可以由用户命名的作用域,用来处理程序中常见的同名冲突. 在 C语言中定义了3个层次的作用域,即文件(编译单元).函数和复合语句.C++ ...

  10. Oracle登录失败:监听程序当前无法识别连接描述符中请求的服务

    Oracle11g下载地址:https://pan.baidu.com/s/1p3RwLUTAl1Ys4yXmXJ3OVQ 安装步骤视频链接:https://pan.baidu.com/s/1c0FC ...