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. Gradle入门实战(Windows版)

    Installation Gradle runs on all major operating systems and requires only a Java JDK or JRE version ...

  2. 阅读MySQL文档第21章摘抄

    触发程序是与表相关的数据库对象. mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2)); Query OK, 0 ro ...

  3. C# Windows服务的安装和卸载批处理

    @ECHO "请按任意键开始安装后台服务. . ."@ECHO "清理原有服务项. . ."%SystemRoot%\Microsoft.NET\Framewo ...

  4. Mac安装composer爬过的坑

    1.首先安装brew/usr/bin/ruby -e "$(curl -fsSLhttps://raw.githubusercontent.com/Homebrew/install/mast ...

  5. matlab中boxplot字体大小设置

    网上找到的:set(findobj(gca,'Type','text'),'FontSize',18) boxplot() uses the default axes labeling for the ...

  6. JSP的域对象的作用范围

    <%-- Created by IntelliJ IDEA. User: tT丶 Date: 2017-12-12 Time: 14:53 To change this template use ...

  7. C语言输入输出函数总结

    常见函数: FILE *p char ch char buf[max] fopen("filename","ab")//打开名为filename的文件并返回一个 ...

  8. synchronized 控制并发(活动秒杀)

    1.首先我们新建一个Controller用于秒杀: package com.imooc.Controller; import com.imooc.service.impl.SeckillService ...

  9. JAVA数据类型能串门不?

    JAVA这几种数据类型,能否串门?入了人家门,就得按人家规矩来,入乡随俗哦,难免发生有自觉的 还有不情愿被动的. 自动类型转换 自动类型转换:容量小的数据类型可以自动转换为容量大的数据类型.在图中,黑 ...

  10. Swift_函数

    Swift_函数 点击查看源码 定义和调用函数 //定义和调用函数 func testDefiningAndCallingFunctions() { func sayHello(_ personNam ...