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].

链接: http://leetcode.com/problems/merge-intervals/

题解:

使用Comparator来对集合进行sort。 Comparator和Comparable是两个很重要的interface,再加上interable,runnable等,要好好掌握。

/**
 * 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<Interval> merge(List<Interval> intervals) {
        List<Interval> res = new ArrayList<>();
        if(intervals == null || intervals.size() == 0)
            return res;
        Collections.sort(intervals, new IntervalComparator());
        
        Interval last = intervals.get(0);
        
        for(int i = 1; i < intervals.size(); i++) {
            Interval curr = intervals.get(i);
            if(last.end < curr.start) {
                res.add(last);
                last = curr;
            } else {
                if(last.end < curr.end)
                    last.end = curr.end;
            }
        }
        
        res.add(last);
        return res;
    }
    
    public class IntervalComparator implements Comparator<Interval> {
        @Override
        public int compare(Interval a, Interval b) {
            return a.start - b.start;
        }
    }
}

 

二刷:

Java:

依然是使用comparator来对整个interval数组进行排序,我们可以用anonymous comparator,或者是Java 8的lambda表达式。 下次把start重命名为prev可能更好一些。

使用Lambda:

Time Complexity - O(nlogn),  Space Complexity - O(1)

/**
 * 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<Interval> merge(List<Interval> intervals) {
        List<Interval> res = new ArrayList<>();
        if (intervals == null || intervals.size() == 0) {
            return intervals;
        }
        Collections.sort(intervals, (Interval i1, Interval i2) -> (i1.start != i2.start ? i1.start - i2.start : i1.end - i2.end));
        Interval start = intervals.get(0);
        for (int i = 1; i < intervals.size(); i++) {
            Interval tmp = intervals.get(i);
            if (start.end < tmp.start) {
                res.add(start);
                start = tmp;
            } else if (start.end < tmp.end){
                start.end = tmp.end;
            }
        }
        res.add(start);
        return res;
    }
}

 

使用Anonymous comparator:

Time Complexity - O(nlogn),  Space Complexity - O(1)

/**
 * 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<Interval> merge(List<Interval> intervals) {
        List<Interval> res = new ArrayList<>();
        if (intervals == null || intervals.size() == 0) {
            return intervals;
        }
        Collections.sort(intervals, new Comparator<Interval>() {
            public int compare(Interval i1, Interval i2) {
                return i1.start != i2.start ? i1.start - i2.start : i1.end - i2.end;
            }
        });
        Interval start = intervals.get(0);
        for (int i = 1; i < intervals.size(); i++) {
            Interval tmp = intervals.get(i);
            if (start.end < tmp.start) {
                res.add(start);
                start = tmp;
            } else if (start.end < tmp.end){
                start.end = tmp.end;
            }
        }
        res.add(start);
        return res;
    }
}

 

Updated:

class Solution {
    public int[][] merge(int[][] intervals) {
        if (intervals == null || intervals.length == 0) return new int[][] {};
        Arrays.sort(intervals, (int[] i1, int[] i2) -> i1[0] != i2[0] ? i1[0] - i2[0] : i1[1] - i2[1]);
        List<int[]> res = new ArrayList<>();
        
        int[] lastInterval = intervals[0];
            
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] <= lastInterval[1]) {
                lastInterval[1] = Math.max(lastInterval[1], intervals[i][1]);
            } else {
                res.add(new int[]{lastInterval[0], lastInterval[1]});
                lastInterval = intervals[i];
            }
        }
        res.add(new int[]{lastInterval[0], lastInterval[1]});
        return res.toArray(new int[res.size()][2]);
    }
}

  

Reference:

https://leetcode.com/discuss/13953/a-simple-java-solution

https://leetcode.com/discuss/33434/a-clean-java-solution

https://leetcode.com/discuss/42344/7-lines-easy-python

https://leetcode.com/discuss/43383/easy-c-solution-with-explanations-o-nlogn-time-and-o-n-space

 

posted @ 2015-04-17 23:42  YRB  阅读(639)  评论(0编辑  收藏  举报