zoukankan      html  css  js  c++  java
  • Intervals

    【LeetCode】

    区间。LeetCode第56题和57题。

    56.  Insert Interval

    下面这种解法是原地插入的方法,显然比新建一个列表省空间。

     1 /* Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
     2 
     3 You may assume that the intervals were initially sorted according to their start times.
     4 
     5 Example 1:
     6 Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
     7 
     8 Example 2:
     9 Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
    10 
    11 This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10]. */
    12 
    13 
    14 /**
    15  * Definition for an interval.
    16  * public class Interval {
    17  *     int start;
    18  *     int end;
    19  *     Interval() { start = 0; end = 0; }
    20  *     Interval(int s, int e) { start = s; end = e; }
    21  * }
    22  */
    23 class Solution {
    24     public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
    25         int i = 0;
    26         while(i < intervals.size() && intervals.get(i).end < newInterval.start)
    27             i++;
    28         while(i < intervals.size() && intervals.get(i).start <= newInterval.end){
    29             newInterval = new Interval(Math.min(intervals.get(i).start, newInterval.start), Math.max(intervals.get(i).end, newInterval.end));
    30             intervals.remove(i);
    31         }
    32         intervals.add(i, newInterval);
    33         return intervals;
    34     }
    35 }

    57 Merge Intervals

    三种解法,三种思路 ,第一种最快。

     1 /* Given a collection of intervals, merge all overlapping intervals.
     2 
     3 For example,
     4 Given [1,3],[2,6],[8,10],[15,18],
     5 return [1,6],[8,10],[15,18]. */
     6 
     7 /**
     8  * Definition for an interval.
     9  * public class Interval {
    10  *     int start;
    11  *     int end;
    12  *     Interval() { start = 0; end = 0; }
    13  *     Interval(int s, int e) { start = s; end = e; }
    14  * }
    15  */
    16  
    17 //Solution1
    18 // The idea is that for the result distinct Interval, the latter one's start must > previous one's end.
    19 class Solution {
    20     public List<Interval> merge(List<Interval> intervals) {
    21         int n = intervals.size();
    22         int[] start = new int[n];
    23         int[] end = new int[n];
    24         for(int i = 0; i < n; i++){
    25             start[i] = intervals.get(i).start;
    26             end[i] = intervals.get(i).end;
    27         }
    28         Arrays.sort(start);
    29         Arrays.sort(end);
    30         List<Interval> res = new ArrayList<>();
    31         for(int i = 0, j = 0; i < n; i++) {
    32             if(i == n - 1 || start[i+1] > end[i]){
    33                 res.add(new Interval(start[j], end[i]));
    34                 j = i + 1;
    35             }
    36         }
    37         return res;
    38     }
    39 }
    40 
    41 //Solution2
    42 //The idea is to sort the intervals by their starting points.
    43 class Solution {
    44     public List<Interval> merge(List<Interval> intervals) {
    45         if(intervals.size() <= 1)
    46             return intervals;
    47         intervals.sort((i1, i2) -> Integer.compare(i1.start, i2.start));
    48         int start = intervals.get(0).start;
    49         int end = intervals.get(0).end;
    50         List<Interval> res = new ArrayList<>();
    51         for(Interval i : intervals){
    52             if(i.start <= end)
    53                 end = Math.max(end, i.end); // attention this line, important
    54             else{
    55                 res.add(new Interval(start, end));
    56                 start = i.start;
    57                 end = i.end;
    58             }
    59         }
    60         res.add(new Interval(start, end));
    61         return res;
    62     }
    63 }
    64 
    65 //Solution3
    66 //use the Insert Interval code, construct a new list, and insert every interval into it
    67 //to reuse the existing code or thoughts is very useful and should take care of it
  • 相关阅读:
    更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found
    解决Xcode 5下使用SVN出现 The operation couldn’t be completed. (NSURLErrorDomain error -1012.) 问题
    Android模拟器启动不了解决办法
    在AndroidManifest.xml文件中设置Android程序的启动界面方法
    Windows2008+MyEclipse10+Android开发环境搭配
    ADT下载地址整理
    android:inputType常用取值
    VS2010中使用Jquery调用Wcf服务读取数据库记录
    Linux手动安装netcore3.0
    StdOS之运行指示灯
  • 原文地址:https://www.cnblogs.com/niuxichuan/p/7487064.html
Copyright © 2011-2022 走看看