LeetCode--057--插入区间(java)
给出一个无重叠的 ,按照区间起始端点排序的区间列表。
在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。
示例 1:
输入: intervals = [[1,3],[6,9]], newInterval = [2,5]
输出: [[1,5],[6,9]]
示例 2:
输入: intervals =[[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval =[4,8]
输出: [[1,2],[3,10],[12,16]]
解释: 这是因为新的区间[4,8]与[3,5],[6,7],[8,10]重叠。
方法1:在56的基础上,采用先插入再合并的方式,过于繁琐,可以直接看方法2
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
if(intervals.length == 0){
int[][] res = new int[1][2];
res[0] = newInterval;
return res;
}
List<int[]> list = new ArrayList();
int temp = -1;//
boolean flag = false;
for(int i = 0;i < intervals.length;i++){
if(newInterval[0] < intervals[i][0]){
flag=true;//当没有找到要插入的位置,必插入末尾
temp = i;
break;
}
}
for(int i = 0;i < intervals.length;i++){
if(i == temp){
list.add(newInterval);
}
list.add(intervals[i]);
}
if(!flag){
list.add(newInterval);
}
int[][] in2 = new int[list.size()][2];
for(int i = 0;i < list.size();i++){
in2[i][0] = list.get(i)[0];
in2[i][1] = list.get(i)[1];
}
return helper(in2);
}
public int[][] helper(int[][] in2){
List<int[]> res = new ArrayList<>();
for(int i = 0;i < in2.length-1;i++ ){
if(in2[i+1][0] <= in2[i][1]){
in2[i+1][0] = in2[i][0];
in2[i+1][1] = Math.max(in2[i+1][1],in2[i][1]);
}else{
res.add(in2[i]);
}
}
res.add(in2[in2.length - 1]);
int[][] res_s =new int[res.size()][2];
for(int i = 0;i < res.size();i++){
res_s[i][0] = res.get(i)[0];
res_s[i][1] = res.get(i)[1];
}
return res_s;
}
}
方法2:
class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> result = new ArrayList<Interval>();
boolean isAddedNew = false;
for(Interval curr : intervals){
if(newInterval.start > curr.end){
//new > curr
result.add(curr);
}else if (newInterval.end < curr.start){
//curr > new
if(!isAddedNew){
result.add(newInterval);
isAddedNew = true;
}
result.add(curr);
}else{
int newStart = curr.start < newInterval.start ?
curr.start : newInterval.start;
int newEnd = curr.end > newInterval.end ?
curr.end : newInterval.end;
newInterval.start = newStart;
newInterval.end = newEnd;
}
}
if(!isAddedNew){
result.add(newInterval);
}
return result;
}
}
2019-05-17 18:14:22
LeetCode--057--插入区间(java)的更多相关文章
- Java实现 LeetCode 57 插入区间
57. 插入区间 给出一个无重叠的 ,按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例 1: 输入: inte ...
- LeetCode 57 插入区间
题目: 给出一个无重叠的 ,按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例 1: 输入: intervals ...
- leetcode 56 合并区间 JAVA
题目: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [ ...
- leetcode 56合并区间 java
//先排序,将左区间小的放在前面,然后如果前一个的右区间大于下一个的左区间,则可以合并,分别用两个下标指向当前的大区间和将要考察的小区间 class Solution { public int[ ...
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- 【LeetCode-面试算法经典-Java实现】【057-Insert Interval(插入区间)】
[057-Insert Interval(插入区间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a set of non-overlapping in ...
- LeetCode:汇总区间【228】
LeetCode:汇总区间[228] 题目描述 给定一个无重复元素的有序整数数组,返回数组区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7] 输出: ["0->2&quo ...
- 【BZOJ】3065: 带插入区间K小值
http://www.lydsy.com/JudgeOnline/problem.php?id=3065 题意:带插入.修改的区间k小值在线查询.(原序列n<=35000, 询问<=175 ...
- lintcode:插入区间
题目: 插入区间 给出一个无重叠的按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 样例 插入区间[2, 5] 到 [ ...
- bzoj 3065: 带插入区间K小值 替罪羊树 && AC300
3065: 带插入区间K小值 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 1062 Solved: 253[Submit][Status] Des ...
随机推荐
- an ordered dict within the ordered dict
w http://stackoverflow.com/questions/20166749/how-to-convert-an-ordereddict-into-a-regular-dict-in-p ...
- 【cs231n作业笔记】二:SVM分类器
可以参考:cs231n assignment1 SVM 完整代码 231n作业 多类 SVM 的损失函数及其梯度计算(最好)https://blog.csdn.net/NODIECANFLY/ar ...
- AtomicReference 源码分析
AtomicReference AtomicReference 能解决什么问题?什么时候使用 AtomicReference? 1)AtomicReference 可以原子更新引用对象. 2)comp ...
- 图书-软件架构:《Design Patterns: Elements of Reusable Object-Oriented Software》(即后述《设计模式》一书)
ylbtech-图书-软件架构:<Design Patterns: Elements of Reusable Object-Oriented Software>(即后述<设计模式&g ...
- python 多进程读写文件
import time from multiprocessing import Process, JoinableQueue, cpu_count import csv ####处理一条数据的方法 d ...
- shell 操作字符串 变量 数组
#!/bin/bash name="jack" #使用双引号拼接 #greeting="hello,"$name"!" #greeting_ ...
- 006-unity3d GUI初识、贴图、自定义鼠标指针
一.gui概念 无论摄像机拍摄到的图像怎么变换,GUI永远显示在屏幕上,不受变形.碰撞.光照的影响.对话框.战斗值.能量等.示例:用手机录像,摄像的参数不会随着拍摄场景变换.GUI基础GUI部分是每帧 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_6_练习_序列化集合
- 将字符串映射为Delphi控件名,批量修改控件属性
http://blog.sina.com.cn/s/blog_4dfbd07c01000a81.html 将字符串映射为Delphi控件名,批量修改控件属性 (2007-10-08 14:50:51) ...
- 如何实现动态水球图 --》 echars结合echarts-liquidfill实现
1)项目中作为项目依赖,安装到项目当中(注意必须要结合echars) npm install echarts vue-echarts --save npm install echarts-liquid ...