# 合并区间

<p>以数组 <code>intervals</code> 表示若干个区间的集合，其中单个区间为 <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 。请你合并所有重叠的区间，并返回一个不重叠的区间数组，该数组需恰好覆盖输入中的所有区间。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>intervals = [[1,3],[2,6],[8,10],[15,18]]<strong><br />输出：</strong>[[1,6],[8,10],[15,18]]<strong><br />解释：</strong>区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>intervals = [[1,4],[4,5]]<strong><br />输出：</strong>[[1,5]]<strong><br />解释：</strong>区间 [1,4] 和 [4,5] 可被视为重叠区间。</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>1 <= intervals.length <= 10<sup>4</sup></code></li>	<li><code>intervals[i].length == 2</code></li>	<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code></li></ul>

## template

```python
class Interval(object):
	def __init__(self, s=0, e=0):
		self.start = s
		self.end = e
class Solution(object):
	def list2interval(self, list_interval):
		ret = []
		for i in list_interval:
			interval = Interval(i[0], i[1])
			ret.append(interval)
		return ret
	def interval2list(self, interval):
		ret = []
		x = [0,0]
		for i in interval:
			x[0] = i.start
			x[1] = i.end
			ret.append(x)
			x = [0,0]
		return ret
	def merge(self, intervals):
		"""
		:type intervals: List[Interval]
		:rtype: List[Interval]
		"""
		if intervals is None:
			return
		ls = len(intervals)
		if ls <= 1:
			return intervals
		intervals = self.list2interval(intervals)		
		intervals.sort(key=lambda x: x.start)
		pos = 0
		while pos < len(intervals) - 1:
			if intervals[pos].end >= intervals[pos + 1].start:
				next = intervals.pop(pos + 1)
				if next.end > intervals[pos].end:
					intervals[pos].end = next.end
			else:
				pos += 1
		intervals = self.interval2list(intervals)
		return intervals
if __name__ == '__main__':
	s = Solution()
	print (s.merge(intervals = [[1,4],[4,5]]))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```