合并重叠区间

Posted

技术标签:

【中文标题】合并重叠区间【英文标题】:Merge Overlapping Intervals 【发布时间】:2015-10-18 16:28:58 【问题描述】:

问题:给定一组任意顺序的时间间隔,将所有重叠的时间间隔合并为一个,并输出应该只有互斥时间间隔的结果。为简单起见,将区间表示为整数对。 例如,让给定的区间集是 1,3, 2,4, 5,7, 6,8 。区间 1,3 和 2,4 相互重叠,因此它们应该合并成为 1, 4。同样5, 7和6, 8应该合并成为5, 8

编写一个函数,为给定的间隔集生成合并的间隔集。

我的代码:

import java.util.*;
import java.lang.*;
import java.io.*;

class Interval 

    int start;
    int end;

    Interval() 
        start = 0;
        end = 0;
    

    Interval(int s, int e) 
    
        start = s;
        end = e;
    


class Ideone

    public ArrayList<Interval> merge(ArrayList<Interval> intervals) 

        if(intervals.size() == 0)
            return intervals;
        if(intervals.size() == 1)
            return intervals;

        Collections.sort(intervals, new IntervalComparator());

        Interval first = intervals.get(0);
        int start = first.start;
        int end = first.end;

        ArrayList<Interval> result = new ArrayList<Interval>();

        for(int i = 1; i < intervals.size(); i++)
            Interval current = intervals.get(i);
            if(current.start <= end)
                end = Math.max(current.end, end);
            else
                result.add(new Interval(start, end));
                start = current.start;
                end = current.end;
            

        

        result.add(new Interval(start, end));

        return result;

    


class IntervalComparator implements Comparator

    public int compare(Object o1, Object o2)
    
        Interval i1 = (Interval)o1;
        Interval i2 = (Interval)o2;
        return i1.start - i2.start;
    

public static void main (String[] args) throws java.lang.Exception

    ArrayList<Interval> x = new ArrayList<Interval>();
    Interval i1 = new Interval(1,3);
    Interval i2 = new Interval(2,6);
    Interval i3 = new Interval(8,10);
    Interval i4 = new Interval(15,18);

    x.add(i1);x.add(i2);x.add(i3);x.add(i4);

    ArrayList<Interval> r = merge(x);

    for(Interval i : r)
    
        System.out.println(i.start+" "+i.end);
    



这些是我编译后遇到的错误,谁能解释一下如何纠正它?

Main.java:69: error: class, interface, or enum expected
public static void main (String[] args) throws java.lang.Exception
              ^
Main.java:72: error: class, interface, or enum expected
    Interval i1 = new Interval(1,3);
    ^
Main.java:73: error: class, interface, or enum expected
    Interval i2 = new Interval(2,6);
    ^
Main.java:74: error: class, interface, or enum expected
    Interval i3 = new Interval(8,10);
    ^
Main.java:75: error: class, interface, or enum expected
    Interval i4 = new Interval(15,18);
    ^
Main.java:77: error: class, interface, or enum expected
    x.add(i1);x.add(i2);x.add(i3);x.add(i4);
    ^
Main.java:77: error: class, interface, or enum expected
    x.add(i1);x.add(i2);x.add(i3);x.add(i4);
              ^
Main.java:77: error: class, interface, or enum expected
    x.add(i1);x.add(i2);x.add(i3);x.add(i4);
                        ^
Main.java:77: error: class, interface, or enum expected
    x.add(i1);x.add(i2);x.add(i3);x.add(i4);
                                  ^
Main.java:79: error: class, interface, or enum expected
        ArrayList<Interval> r = merge(x);
        ^
Main.java:81: error: class, interface, or enum expected
        for(Interval i : r)
        ^
Main.java:84: error: class, interface, or enum expected
        
        ^
12 errors

【问题讨论】:

main 方法应该在一个类中。并且最好在名称与您的 java 文件名相同的类中。 task 很有趣,但问题是关于一个奇怪的基本编译器错误。我想知道赞成票来自哪里...... 【参考方案1】:

Ideone.java:

import java.util.*;

public class Ideone
   
    public static void main (String[] args) throws java.lang.Exception
    
        ArrayList<Interval> x = new ArrayList<>();

        x.add(new Interval(1, 3));
        x.add(new Interval(2, 6));
        x.add(new Interval(8, 10));
        x.add(new Interval(15, 18));
        x.add(new Interval(17, 20));

        x = merge(x);

        for(Interval i : x)
        
            System.out.println(i.getStart() + " " + i.getEnd());
        
    

    public static ArrayList<Interval> merge(ArrayList<Interval> intervals) 

        if(intervals.size() == 0 || intervals.size() == 1)
            return intervals;

        Collections.sort(intervals, new IntervalComparator());

        Interval first = intervals.get(0);
        int start = first.getStart();
        int end = first.getEnd();

        ArrayList<Interval> result = new ArrayList<Interval>();

        for (int i = 1; i < intervals.size(); i++) 
            Interval current = intervals.get(i);
            if (current.getStart() <= end) 
                end = Math.max(current.getEnd(), end);
             else 
                result.add(new Interval(start, end));
                start = current.getStart();
                end = current.getEnd();
            
        

        result.add(new Interval(start, end));
        return result;
    


class Interval 

    private int start;
    private int end;

    Interval() 
        start = 0;
        end = 0;
    

    Interval(int s, int e) 
    
        start = s;
        end = e;
    

    public int getStart() 
        return start;
    

    public int getEnd() 
        return end;
    


class IntervalComparator implements Comparator<Interval>

    public int compare(Interval i1, Interval i2)
    
        return i1.getStart() - i2.getStart();
    

main 方法在 public class IdeoneIntervalIntervalComparator 只是内部类

输出:

1 6
8 10
15 20

【讨论】:

【参考方案2】:

这是您的精炼代码:

import java.util.*;
import java.lang.*;
import java.io.*;

class Interval 
    int start;
    int end;

    Interval() 
        start = 0;
        end = 0;
    

    Interval(int s, int e) 
        start = s;
        end = e;
    


public class Ideone 
    public static void main(String[] args) throws java.lang.Exception 
        ArrayList<Interval> x = new ArrayList<Interval>();
        Interval i1 = new Interval(1, 3);
        Interval i2 = new Interval(2, 6);
        Interval i3 = new Interval(8, 10);
        Interval i4 = new Interval(15, 18);

        x.add(i1);
        x.add(i2);
        x.add(i3);
        x.add(i4);

        ArrayList<Interval> r = merge(x);

        for (Interval i : r) 
            System.out.println(i.start + " " + i.end);
        

    

    public static ArrayList<Interval> merge(ArrayList<Interval> intervals) 

        if (intervals.size() == 0)
            return intervals;
        if (intervals.size() == 1)
            return intervals;

        Collections.sort(intervals, new IntervalComparator());

        Interval first = intervals.get(0);
        int start = first.start;
        int end = first.end;

        ArrayList<Interval> result = new ArrayList<Interval>();

        for (int i = 1; i < intervals.size(); i++) 
            Interval current = intervals.get(i);
            if (current.start <= end) 
                end = Math.max(current.end, end);
             else 
                result.add(new Interval(start, end));
                start = current.start;
                end = current.end;
            

        

        result.add(new Interval(start, end));

        return result;

    


class IntervalComparator implements Comparator 
    public int compare(Object o1, Object o2) 
        Interval i1 = (Interval) o1;
        Interval i2 = (Interval) o2;
        return i1.start - i2.start;
    

并将这个 java 文件命名为“Ideone.java”

【讨论】:

【参考方案3】:

我使用 BST 的实现。 O(logn) 合并:中序遍历为您提供更新的间隔。

private static Interval add(Interval root, Interval x)
    if(root == null)
        return x;
    if(root.start >= x.start)
        root.left = add(root.left,x);
        if(mergeLeft(root,root.left))
            root.left = null;
                       
    
    else
        root.right = add(root.right,x);
        if(mergeRight(root,root.right))
            root.right = null;
        
    

    return root;


private static boolean mergeLeft(Interval root,Interval left)
    if(left.end < root.start)
        return false;

    else
        root.start = Math.min(root.start, left.start);
        root.end = Math.max(root.end, left.end);
        return true;
    


private static boolean mergeRight(Interval root,Interval right)
    if(right.start > root.end)
        return false;
    else
        root.start = Math.min(root.start, right.start);
        root.end = Math.max(root.end, right.end);
        return true;
    

【讨论】:

合并失败时为什么要让root.left/root.right为空?【参考方案4】:

我宁愿把merge方法这样放:

static List<Interval> merge(List<Interval> iList) 
    Collections.sort(iList, new Comparator<Interval>() 
        @Override
        public int compare(Interval o1, Interval o2) 
            return o1.startTime - o2.startTime;
        
    );

    Interval prevIntvl = iList.get(0);
    List<Interval> res = new ArrayList<>();
    for (int i = 1; i < iList.size(); i++) 
        Interval curIntvl = iList.get(i);
        if (curIntvl.startTime < prevIntvl.endTime) 
            prevIntvl.setEndTime(prevIntvl.endTime > curIntvl.endTime ? prevIntvl.endTime : curIntvl.endTime);
         else 
            res.add(prevIntvl);
            prevIntvl = curIntvl;
        
    

    res.add(prevIntvl);

    return res;

【讨论】:

【参考方案5】:
class Solution 
    public List<Interval> merge(List<Interval> intervals) 
        if (intervals.size() <= 1) 
            return intervals;
        
        intervals.sort((i1, i2) -> Integer.compare(i1.start, i2.start));
        List<Interval> result = new LinkedList<Interval>();
        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 
                result.add(new Interval(start, end));
                start = interval.start;
                end = interval.end;
            
        
        result.add(new Interval(start, end));
        return result;
    

【讨论】:

以上是关于合并重叠区间的主要内容,如果未能解决你的问题,请参考以下文章

2021-09-28:合并区间。以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回一个不重叠

Leetcode——重叠区间(会议室1,2) / 合并区间 / 插入区间

56. 合并区间

leetcode| 56. 合并区间

Leetcode 56. 合并区间

力扣leetcode 56. 合并区间