LeetCode 56. Merge Intervals(合并区间)

原题网址:https://leetcode.com/problems/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].

方法:按照开始点对区间进行排序,然后合并。

/**
 * 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) {
        Collections.sort(intervals, new Comparator() {
            @Override
            public int compare(Interval i1, Interval i2) {
                return Integer.compare(i1.start, i2.start);
            }
        });
        List merged = new ArrayList<>();
        for(int i=0; i


你可能感兴趣的:(区间,排序,连续,合并)