LeetCode56. Merge Intervals

原创 2018年04月16日 20:36:46

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

这道题就类似于区间调度,题目还是比较简单的,但是在做的过程中涉及到了自定义类的比较,我也做了相应的整理

就拿这道题来说吧。

intervals.sort((i1, i2) -> Integer.compare(i1.start, i2.start)); 
Collections.sort(intervals, (a,b) -> a.start - b.start);
Collections.sort(intervals, new Comparator<Interval>() {
		public int compare(Interval i1, Interval i2) {
			if (i1.start != i2.start) {
				return i1.start - i2.start;
			}
			return i1.end - i2.end;
		}
	});

这三种方法都能用来比较大小,我倾向于第一种。

下面是java代码:

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> res=new ArrayList<>();
        int starttime = intervals.get(0).start;
        int endtime = intervals.get(0).end;
        for(Interval i:intervals)
        {
            if(i.start<=endtime)
            {
                endtime=Math.max(i.end,endtime);
            }
            else
            {
                Interval in=new Interval(starttime,endtime);
                res.add(in);
                starttime=i.start;
                endtime=i.end;
            }
        }
        res.add(new Interval(starttime, endtime));
        return res;
    }
}
还有一个小点就是最后一个情况不要忘了就行。

Git入门

-
  • 1970年01月01日 08:00

Leetcode56. Merge Intervals

56. Merge Intervals(合并重复区间) Given a collection of intervals, merge all overlapping intervals. For...
  • feng111133
  • feng111133
  • 2017-08-25 19:04:32
  • 52

leetcode56. Merge Intervals

56. Merge IntervalsGiven a collection of intervals, merge all overlapping intervals.For example, Gi...
  • rxt2012kc
  • rxt2012kc
  • 2017-04-05 13:39:45
  • 86

[leetcode-56]Merge Intervals(java)

问题描述: Given a collection of intervals, merge all overlapping intervals.For example, Given [1,3],[2...
  • zdavb
  • zdavb
  • 2015-08-03 10:28:32
  • 965

【LeetCode】Merge Intervals 解题报告

【题目】 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[...
  • ljiabin
  • ljiabin
  • 2015-04-06 20:32:50
  • 4371

[Python]Merge Intervals

Given a collection of intervals, merge all overlapping intervals.For example, Given [1,3],[2,6],[8,...
  • ghui23
  • ghui23
  • 2016-06-15 23:20:31
  • 514

LeetCode 56:Merge Intervals

Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8...
  • sunao2002002
  • sunao2002002
  • 2015-06-02 21:17:41
  • 1925

Merge Intervals -- LeetCode

原题链接: http://oj.leetcode.com/problems/merge-intervals/  这是一道关于interval数组结构的操作,在面试中也是一种比较常见的数据结构。假设这...
  • linhuanmars
  • linhuanmars
  • 2014-03-23 08:16:45
  • 11820

LeetCode-56-Merge Intervals(C语言实现)

/**  * Definition for an interval.  * struct Interval {  *     int start;  *     int end;  * };...
  • AI_lalaland
  • AI_lalaland
  • 2018-01-03 07:15:54
  • 77

leetCode 56.Merge Intervals (合并区间) 解题思路和方法

Merge Intervals  Given a collection of intervals, merge all overlapping intervals. For example...
  • xygy8860
  • xygy8860
  • 2015-07-15 14:06:02
  • 1616
收藏助手
不良信息举报
您举报文章:LeetCode56. Merge Intervals
举报原因:
原因补充:

(最多只允许输入30个字)