插入区间

给出一个无重叠的按照区间起始端点排序的区间列表。

在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。

样例

插入区间[2, 5] 到 [[1,2], [5,9]],我们得到 [[1,9]]

插入区间[3, 4] 到 [[1,2], [5,9]],我们得到 [[1,2], [3,4], [5,9]]

 1 /**
 2  * Definition of Interval:
 3  * public classs Interval {
 4  *     int start, end;
 5  *     Interval(int start, int end) {
 6  *         this.start = start;
 7  *         this.end = end;
 8  *     }
 9  */
10
11 class Solution {
12     /**
13      * Insert newInterval into intervals.
14      * @param intervals: Sorted interval list.
15      * @param newInterval: A new interval.
16      * @return: A new sorted interval list.
17      */
18     public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
19         // write your code here
20         if(intervals == null || newInterval == null ){
21             return intervals;
22         }
23         if(intervals.size()==0){
24             intervals.add(newInterval);
25             return intervals;
26         }
27         ListIterator<Interval>  it = intervals.listIterator();
28         while(it.hasNext()){
29             Interval temInterval = it.next();
30             if(newInterval.end < temInterval.start){
31                 it.previous();
32                 it.add(newInterval);
33                 return intervals;
34             }else{
35                 if(temInterval.end<newInterval.start){
36                     continue;
37                 }else{
38                     newInterval.start = Math.min(temInterval.start,newInterval.start);
39                     newInterval.end  =  Math.max(temInterval.end,newInterval.end);
40                     it.remove();
41                 }
42             }
43         }
44         intervals.add(newInterval);
45         return intervals;
46     }
47 }
时间: 2024-08-13 18:36:35

插入区间的相关文章

[BZOJ3065]带插入区间K小值 解题报告 替罪羊树+值域线段树

刚了一天的题终于切掉了,数据结构题的代码真**难调,这是我做过的第一道树套树题,做完后感觉对树套树都有阴影了......下面写一下做题记录. Portal Gun:[BZOJ3065]带插入区间k小值. 这道题的题面其实都提醒怎么做了,维护区间k小值用值域线段树,但要维护一个插入操作,树状数组套主席树也用不了,那么这道题还剩下平衡树可以搞,那就上平衡树吧. 我这里的做法,因为要维护序列的顺序,所以我这里用到替罪羊树套值域线段树:我们在替罪羊树的每个节点都套一颗值域线段树,记录以该节点为根的子树的

bzoj 3065: 带插入区间K小值 替罪羊树 &amp;&amp; AC300

3065: 带插入区间K小值 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1062  Solved: 253[Submit][Status] Description 从 前有n只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力a[i].跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理性愉悦一 下,查询区间k小值.他每次向它的随从伏特提出这样的问题: 从左往右第x个到第y个跳蚤中,a[i]第k小的值是多少. 这可难不倒伏特,

【BZOJ】3065: 带插入区间K小值

题意:带插入.修改的区间k小值在线查询.(原序列n<=35000, 询问<=175000) #include <bits/stdc++.h> using namespace std; const int nTr=1000005, nSg=15000005, alphaA=4, alphaB=5; int STop; struct Seg *Snull; struct Seg { Seg *l, *r; int s, cnt; }Sg[nSg], *iSg=Sg, *bin[nSg]

【LeetCode-面试算法经典-Java实现】【057-Insert Interval(插入区间)】

[057-Insert Interval(插入区间)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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 star

插入区间--Insert Intervals

https://leetcode.com/problems/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 t

LintCode 30插入区间

问题 给出一个无重叠的按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 样例 插入区间[2, 5] 到 [[1,2], [5,9]],我们得到 [[1,9]]. 插入区间[3, 4] 到 [[1,2], [5,9]],我们得到 [[1,2], [3,4], [5,9]]. 思路 只要依次遍历,判断当前元素与要插入元素的关系. 如当前元素的右端点小于插入元素的左端点,则说明当前元素与插入元素无交并. 如当前元素的左端

LintCode 30. 插入区间

题目: 给出一个无重叠的按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 样例 插入区间[2, 5] 到 [[1,2], [5,9]],我们得到 [[1,9]]. 插入区间[3, 4] 到 [[1,2], [5,9]],我们得到 [[1,2], [3,4], [5,9]]. 解: 分三种情况: 第一个if:插入的end比原区间的第i个元素的start小(即自己最大的比人家最小的还要小,就放到人家前面),例: 插入区

LeetCode 57. 插入区间

题目链接:https://leetcode-cn.com/problems/insert-interval/ 解法一:可以LeetCode 56 题的合并区间为基础.   将newInterval插入至intervals中,然后对intervals进行合并区间,就能够得到最终的结果.   时间复杂度:O(N) 解法二:贪心算法. 配合代码讲解: 使用start_ptr和end_ptr记录需要被合并的块区间. 用start_pos和end_pos记录合并的区间的端点值. 使用start_flag来

【python-leetcode57-区间合并】插入区间

问题描述: 给出一个无重叠的 ,按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例 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]