# 56. Merge Intervals 合并区间

@TOC

## # 题目描述

Given a collection of intervals, merge all overlapping intervals.

Example 1:

``````Input: [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
``````

Example 2:

``````Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
``````

## # 解题方法

python代码如下：

``````# Definition for an interval.
# class Interval(object):
#     def __init__(self, s=0, e=0):
#         self.start = s
#         self.end = e

class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
N = len(intervals)
if not N: return []
intervals.sort(key = lambda x : x.start)
res = []
start = intervals[0].start
end = intervals[0].end
for it in intervals:
if it.start <= end:
end = max(end, it.end)
else:
cur = Interval(start, end)
res.append(cur)
start = it.start
end = it.end
res.append(Interval(start, end))
return res
``````

Java代码如下：

``````/**
* 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) {
if (intervals.size() <= 1) {
return intervals;
}
Collections.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval o1, Interval o2) {
return o1.start - o2.start;
}
});
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 {
start = interval.start;
end = interval.end;
}
}
}
}
``````

C++代码如下：

``````/**
* Definition for an interval.
* struct Interval {
*     int start;
*     int end;
*     Interval() : start(0), end(0) {}
*     Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<Interval> merge(vector<Interval>& intervals) {
const int N = intervals.size();
vector<Interval> res;
if (N == 0) return res;
sort(intervals.begin(), intervals.end(), [](Interval a, Interval b){
return a.start < b.start;
});
int start = intervals[0].start;
int end = intervals[0].end;
for (auto& it : intervals) {
if (it.start <= end) {
end = max(it.end, end);
} else {
res.push_back(Interval(start, end));
start = it.start;
end = it.end;
}
}
res.push_back(Interval(start, end));
return res;
}
};
``````

## # 日期

2017 年 4 月 4 日 2019 年 1 月 9 日 —— 抓紧时间学习啊！