lintcode 容易题:Merge Intervals 合并区间

题目:

合并区间

给出若干闭合区间,合并所有重叠的部分。

样例

给出的区间列表 => 合并后的区间列表:

[                     [
  [1, 3],               [1, 6],
  [2, 6],      =>       [8, 10],
  [8, 10],              [15, 18]
  [15, 18]            ]
]挑战

O(n log n) 的时间和 O(1) 的额外空间。

解题:

先以区间的左边界进行排序,再异步的方式比较右边边界进行合并

程序来源

Java程序

/**
 * Definition of Interval:
 * public class Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this.start = start;
 *         this.end = end;
 *     }
 */

class Solution {
    /**
     * @param intervals: Sorted interval list.
     * @return: A new sorted interval list.
     */
    public List<Interval> merge(List<Interval> intervals) {
        // write your code here
        if(intervals == null || intervals.size()<=1){
            return intervals;
        }
        Collections.sort(intervals,new IntervalComparator());
        List<Interval> result = new ArrayList<Interval>();
        Interval last = intervals.get(0);
        for(int i=1;i<intervals.size();i++){
            Interval curt = intervals.get(i);
            if(curt.start<=last.end){
                last.end = Math.max(last.end,curt.end);
            }else{
                result.add(last);
                last = curt;
            }
        }
        result.add(last);
        return result;
    }
    private class IntervalComparator implements Comparator<Interval>{
        public int compare(Interval a,Interval b){
            return a.start - b.start;
        }
    }

}

总耗时: 2250 ms

Python程序

时间: 2024-10-11 05:50:25

lintcode 容易题:Merge Intervals 合并区间的相关文章

leetCode 56.Merge Intervals (合并区间) 解题思路和方法

Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 思路:题意很明确,首先对各区间按开始来排序,最后遍历,如果前面和后面的区间有重合,合并. 具体代码: /** * Definition for an interval. * publi

[LeetCode] 56 - Merge Intervals 合并区间

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 思路: 我们首先要做的就是给区间集排序,由于我们要排序的是个结构体,所以我们要定义自己的comparator,才能用sort来排序,我们以start的值从小到大来排序,排完序我们就可以开始合并了,首先把第一个区间存入结果

[Leetcode] merge intervals 合并区间

Given a collection of intervals, merge all overlapping intervals. For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18]. 题意:给定一系列区间,合并重叠区间 思路:其实,个人觉得,题目的例子有一定误导性,以为区间已经按每个区间的start值排好序了.结果,毫无疑问,悲剧了.若是已经排好序,则,只需将当前区间的end值和下一个的start值比较,

(Java) LeetCode 56. Merge Intervalse —— 合并区间

Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. Example 2: Input: [[1,4],[4,

【LeetCode】Merge Intervals 题解 利用Comparator进行排序

题目链接Merge Intervals /** * 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; } * } * 题目:LeetCode 第56题 Merge Intervals 区间合并给定一个区间的集合,将相邻区间之间

LeetCode 56. Merge Intervals (合并区间)

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 题目标签:Array 这道题目给了我们一个区间的list,让我们返回一个list,是合并了所有有重叠的区间之后的list.这道题目的关键在于如何判断两个区间有重叠,根据原题给的例子可以看出,在按照每个区间的start排序

合并区间--Merge Intervals

https://leetcode.com/problems/merge-intervals/ Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 题意:合并简化区间列 1 # Definition for an interval. 2 #

【LeetCode-面试算法经典-Java实现】【056-Merge Intervals(区间合并)】

[056-Merge Intervals(区间合并)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 题目大意 给定一个区间集合,合并有重叠的区间. 解题思路 先对区间进行排序.按開始

【leetcode刷题笔记】Merge Intervals

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 题解:首先对所有的区间按照start大小排序,然后遍历排序后的数组,用last记录前一个区间,如果遍历的当前区间可以和last合并,就把它合并到last里面:否则就把last放到answer list中,并且更新last