【LeetCode题目记录-4】插入数组间隔问题
来源:互联网 发布:mysql 建表 索引 编辑:程序博客网 时间:2024/06/05 11:42
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].
【分析-非原创】
参考:https://oj.leetcode.com/discuss/3971/in-place-solution-ask-for-suggestion
public static List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> list=new ArrayList<Interval>();
for(int i=0;i<intervals.size();i++){
/*只要在newInterval左边的都直接加入到list*/
if(newInterval.start>intervals.get(i).end){
list.add(intervals.get(i));
/*只要在newInterval在的右边,就将newInterval加入list,同时将右边这个当做newInterval*/
}else if(newInterval.end<intervals.get(i).start){
list.add(newInterval);
newInterval=intervals.get(i);
}else{
/*获取新的newInterval*/
newInterval.start=Math.min(newInterval.start,intervals.get(i).start);
newInterval.end=Math.max(newInterval.end,intervals.get(i).end);
}
}
list.add(newInterval);
return list;
}
- 【LeetCode题目记录-4】插入数组间隔问题
- 【LeetCode】数组系列(间隔)
- leetcode题目记录
- Leetcode数组类题目
- LeetCode 题目之 数组两数之和问题
- Leetcode #57. Insert Interval 间隔插入 解题报告
- leetcode 题目记录:7,8
- 【LeetCode题目记录-9】排序后的数组生成平衡的二叉搜索树
- 【LeetCode题目记录-10】已排序的数组去除重复元素
- 【LeetCode题目记录-13】二分搜索排序后的二维数组
- Java编程题目-4:数组操作问题
- leetcode 数组类 所有题目
- leetcode编程中数组题目
- 57.插入间隔
- 插入记录的顺序问题!
- leetcode 刷题目,总结,记录,备忘 13
- leetcode刷题目 ,总结,记录,备忘206
- leetcode刷题目 总结 记录 备忘11
- TIM输出比较的三种模式
- Protobuffor Java使用说明(包含nano、micro版本)附件protobuf-java-2.3.0.jar
- volatile
- HTML5添加 video 视频标签后仍然无法播放的解决方法
- 浅析Java中的final关键字
- 【LeetCode题目记录-4】插入数组间隔问题
- 读《周鸿祎自述:我的互联网方法论》有感
- html 欣赏
- 【Unix/Linux】【命令】【文件,目录操作工具】复制文件或目录——cp命令
- C++之复合类型
- intent的调用
- 电话吸费代理 手机吸费赚钱 小成本利润高
- Oracle性能分析8:使用索引
- android Fragment中没有onTouchEvent解决方法