leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval
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].
思路:插入合并就可以,相比于上一题。这题还要简单一些。
详细代码例如以下:
/**
* 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> list = new ArrayList<Interval>();
//边界情况
if(intervals.size() == 0){
list.add(newInterval);
return list;
}
//循环推断
for(int i = 0; i < intervals.size();i++){
//假设新的区间结束值小于開始值。则直接插入前面。后面依次插入就可以
if(newInterval.end < intervals.get(i).start){
list.add(newInterval);
for(int j = i; j < intervals.size(); j++){
list.add(intervals.get(j));
}
break;
}
//新的区间開始点大于结束点。则当前点直接加入结果集
else if(newInterval.start > intervals.get(i).end){
list.add(intervals.get(i));
}
//须要合并的情况
else{
//合并区间
newInterval.start = Math.min(newInterval.start,intervals.get(i).start);
newInterval.end = Math.max(newInterval.end,intervals.get(i).end);
}
if(i == intervals.size() - 1){//假设是最后一个数据。也加入结果集中
list.add(newInterval);
}
}
return list;
}
}
leetCode 57.Insert Interval (插入区间) 解题思路和方法的更多相关文章
- [leetcode]57. Insert Interval插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode] 57. Insert Interval 解决思路
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode: 57. Insert Interval(Hard)
1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...
- 第一周 Leetcode 57. Insert Interval (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- 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 ...
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 057 Insert Interval 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...
随机推荐
- Java:网络编程之应用实例
1.聊天 编写一个聊天程序,有收数据的部分和发数据的部分. 这两部分需要同时执行,那么就需要多线程技术. 一个线程控制发送. 一个线程控制接收. 因为收和发动作是不一致的,所以要定义两个run方法,而 ...
- 将ascll码转换成数值进行运算
#include "stdlib.h"#include "stdio.h"int main() { char a[8] = { 49,32,33,61,62,6 ...
- redis学习笔记——入门
基本安装和用法:http://www.tuicool.com/articles/QzMRNb Redis如何通过本机客户端访问远程服务器段:http://blog.sina.com.cn/s/blog ...
- java.lang.IllegalArgumentException: Wrong state classs
java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class cn.et ...
- 动态添加Fragment
在Fragment简单用法的基础上做修改 一.新建:another_right_fragment.xml <LinearLayout xmlns:android="http://sch ...
- 【海盗派测试分析MFQ&PPDCS】读书笔记
使用脑图花了一张读书笔记,可能有点长
- Spring使用经验之StandardServletMultipartResolver实现文件上传的基本配置
Note:Spring使用版本是4.1.6.RELEASE 1. 在实现了AbstractAnnotationConfigDispatcherServletInitializer的类中重载custom ...
- Git常用统计命令
上周要做个汇报PPT涉及到个人对项目贡献量,在网上搜集了些常用统计命令,总结如下: 1.统计代码提交量(包括添加.删除): git log --author="$(gitconfig--ge ...
- ASP.NET基本对象介绍
ASP.NET能够成为一个庞大的软件体系,与它提供了大量的对象类库有很大的关系.这些类库中包含许多封装好的内置对象,开发人员可以直接使用这些对象的方法和属性,因此用较少的代码量就能轻松完成很多对象. ...
- UVa 437 The Tower of Babylon(DP 最长条件子序列)
题意 给你n种长方体 每种都有无穷个 当一个长方体的长和宽都小于还有一个时 这个长方体能够放在还有一个上面 要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法 比較不好控制 ...