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

struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};

class Solution {
public:
static bool comp(const Interval a, const Interval b)
{
return a.start < b.start ? 1 : 0;
}
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> result;
int size = intervals.size();
if (size < 2) return intervals;
sort(intervals.begin(), intervals.end(), comp);
for (int i = 0; i<size; ++i)
{
Interval temp(intervals[i].start, intervals[i].end);
int high = intervals[i].end;
while (i < size - 1 && high >= intervals[i + 1].start)
{
if (intervals[i + 1].end > high) high = intervals[i + 1].end;
++i;
}
temp.end = high;
result.push_back(temp);
}
return result;
}
};

LeetCode 56:Merge Intervals

2015-06-02 21:17:41

LeetCode 56 Merge Intervals（Python实现及详解）

2017-08-01 11:27:49

56. Merge Intervals-leetcode-java

2016-05-06 18:48:42

LeetCode 56 Merge Intervals--In C++

2016-05-23 10:49:25

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

2015-07-15 14:06:02

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

2018-01-03 07:15:54

[leetcode-56]Merge Intervals(java)

2015-08-03 10:28:32

LeetCode --- 56. Merge Intervals

2015-03-14 23:03:47

【LeetCode】Merge Intervals 解题报告

2015-04-06 20:32:50

Java Merge Intervals(合并间隔)

2014-07-17 16:44:34

不良信息举报

leetcode 56:Merge Intervals