资讯专栏INFORMATION COLUMN

merge Interval

terro / 2073人阅读

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
# 1 based on start  26ms
/**
 * 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; }
 * }
 */
public class Solution {
    public List merge(List intervals) {
        List res = new ArrayList();
        if(intervals == null || intervals.size() <= 1) return intervals;
        Collections.sort(intervals, new Comparator(){
            public int compare(Interval a, Interval b){
                return a.start - b.start;
            }
        });
        
        int start = intervals.get(0).start;
        int end = intervals.get(0).end;
        
        for(Interval interval : intervals){
            if(interval.start <= end){
                end = Math.max(end, interval.end);
            } else {
                res.add(new Interval(start, end));
                start = interval.start;
                end = interval.end;
            }
        }
        res.add(new Interval(start, end));
   
        return res;
    }
}
# 2 based on end  24ms
public class Solution {
    public List merge(List intervals) {
        List res = new ArrayList();
        if(intervals == null || intervals.size() <= 1) return intervals;
        Collections.sort(intervals, new Comparator(){
            public int compare(Interval a, Interval b){
                return b.end - a.end;
            }
        });
        
        Interval last = intervals.get(0);
        Interval cur = null;
        for(int i = 1; i < intervals.size(); i++){
            cur = intervals.get(i);
            if(last.start > cur.end){
                res.add(last);
                last = cur;
            } else {
                last.start = Math.min(cur.start, last.start);
            }
        }
        res.add(last);
        
        Collections.reverse(res);
        
        return res;
    }
}
# number of meeting rooms, 拆分start,end  20ms
public class Solution {
    public List merge(List intervals) {
        int n = intervals.size();
        int[] starts = new int[n];
        int[] ends = new int[n];
        for(int i=0; i res = new ArrayList();
        for(int i=0, j=0; i ends[i]){  // next meeting starts after this ends, cant merge
                res.add(new Interval(starts[j], ends[i]));
                j = i + 1;
            }
        }
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70032.html

相关文章

  • [Leetcode] Merge Intervals and Insert Interval 合并间

    摘要:我们只要把所有和该有重叠的合并到一起就行了。最后把前半部分的列表,合并后的大和后半部分的列表连起来,就是结果了。 Merge Intervals 最新更新请见 https://yanjia.me/zh/2019/02/... Given a collection of intervals, merge all overlapping intervals.For example, Gi...

    antyiwei 评论0 收藏0
  • [LeetCode/LintCode] Merge Intervals

    摘要:方法上没太多难点,先按所有区间的起点排序,然后用和两个指针,如果有交集进行操作,否则向后移动。由于要求的,就对原数组直接进行操作了。时间复杂度是的时间。 Problem Given a collection of intervals, merge all overlapping intervals. Example Given intervals => merged intervals...

    gougoujiang 评论0 收藏0
  • leetcode--57--Insert Interval

    摘要:问题描述分析这道题的关键在于理解问题,抽取原型,理解中间可以部分如何界定,以及非部分如何进行追加。需要注意的是循环到最后一个元素和在最后一个元素的区别。 问题描述: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You m...

    kycool 评论0 收藏0
  • 【LC总结】Intervals题目 (Insert/Merge/Number of Airplane

    摘要:忘了这题怎么做,汗颜无地。边界用记录每个时刻的飞行数目。对于某一时刻,起飞和降落同时发生,只计算一次。先强势插入,再 Merge Intervals Problem Given a collection of intervals, merge all overlapping intervals. Example Given intervals => merged intervals: ...

    Achilles 评论0 收藏0
  • leetcode56 Merge Intervals

    摘要:题目要求输入一系列区间,将出现交叉的区间合并起来,并将合并后的区间返回。将这两个数组分别排序可以得到和。也就是说,合并后的结果和是等价的。举个栗子,输入为,先判断是否是独立区间,可见可以和合并,则将修改为。 题目要求 Given a collection of intervals, merge all overlapping intervals. For example, Given...

    shmily 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<