zoukankan      html  css  js  c++  java
  • 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排序之后很容易判断出是否相邻的两个区间有交集。我们看第一个区间[1,3] 中的end 3 > 第二个区间[2,6] 的start 2。在排序完之后,只要前面一个区间的end 大于等于 后面一个区间的start,它们就是重叠的。那么为了merge 这两个区间,保留第一个区间的start, 在两个end里面拿大的。设一个temp 等于第一个区间,遍历剩下的区间。然后每次拿temp 和这个区间比较,要注意的是,如果遇到了重叠的,把temp 更新为merge 后的区间,这时不需要加入list,因为还有可能继续和下一个区间重叠。当temp和这个区间不重叠的时候,把temp 加入list,再把这个区间设为新的temp。

    Java Solution:

    Runtime beats 89.25% 

    完成日期:07/20/2017

    关键词:Array

    关键点:利用comparator sort list,当A的end 大于等于 B的start,说明重叠

     1 /**
     2  * Definition for an interval.
     3  * public class Interval {
     4  *     int start;
     5  *     int end;
     6  *     Interval() { start = 0; end = 0; }
     7  *     Interval(int s, int e) { start = s; end = e; }
     8  * }
     9  */
    10 public class Solution 
    11 {
    12     public List<Interval> merge(List<Interval> intervals) 
    13     {
    14         List<Interval> res = new ArrayList<>();
    15         if(intervals.size() == 0)
    16             return res;
    17         
    18         // sort the intervals list according to start
    19         Collections.sort(intervals, new Comparator<Interval>(){
    20             public int compare(Interval a, Interval b)
    21             {
    22                 return a.start - b.start;
    23             }
    24         });;
    25         
    26         // get first one
    27         Interval temp = intervals.get(0);
    28         
    29         // if intervals only has one element
    30         if(intervals.size() == 1)
    31         {
    32             res.add(temp);
    33             return res;
    34         }
    35         
    36         // iterate intervals from [1] to end
    37         for(int i=1; i<intervals.size(); i++)
    38         {
    39             // case 1: if temp interval end >= this interval start -> 
    40             //            merge tempStart, max(tempEnd, thisEnd) and make this merge one as new temp;
    41             if(temp.end >= intervals.get(i).start)
    42             {
    43                 temp.end = Math.max(temp.end, intervals.get(i).end);
    44             }
    45             // case 2: if temp interval is not overlapping this interval ->
    46             //            add temp into list and make this new temp
    47             else
    48             {
    49                 res.add(temp);
    50                 temp = intervals.get(i);
    51             }
    52         }
    53         // add the last temp into list
    54         res.add(temp);
    55         
    56 
    57         return res;
    58     }
    59 }

    参考资料:N/A

    LeetCode 算法题目列表 - LeetCode Algorithms Questions List

  • 相关阅读:
    Android笔记(六十六) android中的动画——XML文件定义属性动画
    Android笔记(六十五) android中的动画——属性动画(propertyanimation)
    python函数参数默认值及重要警告
    Python 列表
    Python数学运算入门把Python当作计算器
    Python 的非正式介绍
    python中为什么 if/while/def/class语句需要冒号?
    为什么Python在列表和元组的末尾允许使用逗号?
    现实世界中的 Python
    Python常见问题
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7215622.html
Copyright © 2011-2022 走看看