156. Merge Intervals【LintCode by java】

Description

Given a collection of intervals, merge all overlapping intervals.

Example

Given intervals => merged intervals:

[                     [
  (1, 3),               (1, 6),
  (2, 6),      =>       (8, 10),
  (8, 10),              (15, 18)
  (15, 18)            ]
]

Challenge

O(n log n) time and O(1) extra space.

题意:给定一个集合,里面有若干无序区间,要求将有重叠部分的区间合并。这个题目的示例给的不是很好,这个示例给人的感觉好像这些区间是有序的。有序和无序,用同样的方法做,结果可能不一样,比如我一开始理解成有序,报错如下:

Input
[(2,3),(4,5),(6,7),(8,9),(1,10)]
Output
[(2,3),(4,5),(6,7),(1,10)]
Expected
[(1,10)]
Hint
Review your code and make sure your algorithm is correct. Wrong answer usually caused by typos if your algorithm is correct.
Input test data (one parameter per line.)

虽然它本来无序,但我们也可以人为地将它根据first值的大小进行排序,可以使用Collections类中的sort方法(查一下API)对List进行排序。排完之后,就可以对集合内的区间进行合并了。合并的方法与此题类似:30. Insert Interval【LintCode by java】

申请一个新的集合,再用一个循环,将排好序的区间两两比较,如果无需合并,则将前者加入新的集合,后者继续与后面的区间比较合并。代码如下:

 1 public class Solution {
 2     /**
 3      * @param intervals: interval list.
 4      * @return: A new interval list.
 5      */
 6      //判断两区间是否相交
 7     public List<Interval> merge(List<Interval> intervals) {
 8         // write your code here
 9         if(intervals.size()==0||intervals.size()==1)
10            return intervals;
11         List<Interval>res=new ArrayList<Interval>();
12         Collections.sort(intervals,new IntervalCompare());
13         Interval last=intervals.get(0);
14         for(int i=1;i<intervals.size();i++){
15             Interval cur=intervals.get(i);
16             if(last.end<cur.start){
17                 res.add(last);
18                 last=cur;
19             }else{
20                 last.start=Math.min(last.start,cur.start);
21                 last.end=Math.max(last.end,cur.end);
22             }
23         }
24         res.add(last);
25         return res;
26     }
27       private class IntervalCompare implements Comparator<Interval>{
28         public int compare(Interval a,Interval b){
29             return a.start-b.start;
30         }
31     }
32 }

如有错误,欢迎批评指正~

原文地址:https://www.cnblogs.com/phdeblog/p/9111599.html

时间: 2024-08-30 00:12:53

156. Merge Intervals【LintCode by java】的相关文章

165. Merge Two Sorted Lists【LintCode by java】

Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order. Example Given 1->3->8->11->15->null,

158. Valid Anagram【LintCode by java】

Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification What is Anagram? Two strings are anagram if they can be the same after change the order of characters. Example Given s = "abcd", t = "dcab&q

212. Space Replacement【LintCode by java】

Description Write a method to replace all spaces in a string with %20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string. You code should also return the new

172. Remove Element【LintCode by java】

Description Given an array and a value, remove all occurrences of that value in place and return the new length. The order of elements can be changed, and the elements after the new length don't matter. Example Given an array [0,4,4,0,0,2,4,4], value

175. Invert Binary Tree【LintCode by java】

Description Invert a binary tree. Example 1 1 / \ / 2 3 => 3 2 / 4 4   解题:题目要求讲二叉树的左子树和右子树对调一下,用递归来做很简单: 1 /** 2 * Definition of TreeNode: 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left, right; 6 * public TreeNode(int val) {

181. Flip Bits【LintCode, by java】

Description Determine the number of bits required to flip if you want to convert integer n to integer m. Both n and m are 32-bit integers. Example Given n = 31 (11111), m = 14 (01110), return 2. 解题:比较两个整数对应的二进制数,共有多少位不同.注意,负数也包含在内.">>>"

MongDB基础学习(七)—— 【MongoDB for Java】Java操作MongoDB

[MongoDB for Java]Java操作MongoDB 开发的产品为了融资,不停得改版,从第一版到现在最新版本,最后发现公司发展方向都变了,有最初电子商务改成VR内容提供者(没办法,要别人钱,就得按照别人的规划的战略走).本来本章节会放到后面再做讲解,无奈,部门需要做一次培训任务,我就想到拿Java操作MongoDB作为培训内容,开发环境和依赖jar如下: (1)开发环境: System:Windows IDE:eclipse Database:mongoDB2.6 Maven:apac

Java编程思想【Thinking in java】

Java编程思想[Thinking in java]目录:第1章 对象导论1.1抽象过程1.2每个对象都有一个接口1.3每个对象都提供服务1.4被隐藏的具体实现1.5复用具体实现1.6继承1.6.1“是一个”(is-a)与“像是一个”(is-like-a)关系1.7伴随多态的可互换对象1.8单根继承结构1.9容器1.9.1参数化类型(范型)1.10对象的创建和生命周期1.11异常处理:处理错误1.12并发编程1.13Java与Internet1.13.1Web是什么1.13.2客户端编程1.13

HDU 1384 Intervals【差分约束-SPFA】

类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b<=k2,c−a<=k3.将a,b,c转换为节点:k1,k2,k3转换为边权:减数指向被减数,形成一个有向图: 由题可得(b−a) + (c−b) <= k1+k2,c−a<=k1+k2.比较k1+k2与k3,其中较小者就是c−a的最大值.由此我们可以得知求差的最大值,即上限被约束,此时我