# 合并区间

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

以下程序实现了这一功能，请你填补空白处内容：

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int compare(const void *a, const void *b)
{
	return ((int *)a)[0] - ((int *)b)[0];
}
int **merge(int **intervals, int intervalsSize, int *intervalsColSize, int *returnSize, int **returnColumnSizes)
{
	if (intervalsSize == 0)
	{
		*returnSize = 0;
		return intervals;
	}
	int i, len = 0;
	int *tmp = malloc(intervalsSize * 2 * sizeof(int));
	for (i = 0; i < intervalsSize; i++)
	{
		tmp[i * 2] = intervals[i][0];
		tmp[i * 2 + 1] = intervals[i][1];
	}
	qsort(tmp, intervalsSize, 2 * sizeof(int), compare);
	intervals[0][0] = tmp[0];
	intervals[0][1] = tmp[1];
	for (i = 1; i < intervalsSize; i++)
	{
		if (tmp[i * 2] > intervals[len][1])
		{
			len++;
			___________________________;
		}
		else if (tmp[i * 2 + 1] > intervals[len][1])
		{
			intervals[len][1] = tmp[i * 2 + 1];
		}
	}
	len += 1;
	*returnSize = len;
	*returnColumnSizes = malloc(len * sizeof(int));
	for (i = 0; i < len; i++)
	{
		(*returnColumnSizes)[i] = 2;
	}
	return intervals;
}
int main(int argc, char **argv)
{
	if (argc < 1 || argc % 2 == 0)
	{
		fprintf(stderr, "Usage: ./test s0 e0 s1 e1...");
		exit(-1);
	}
	int i, count = 0;
	int *sizes = malloc((argc - 1) / 2 * sizeof(int));
	int **intervals = malloc((argc - 1) / 2 * sizeof(int *));
	for (i = 0; i < (argc - 1) / 2; i++)
	{
		sizes[i] = 2;
		intervals[i] = malloc(2 * sizeof(int));
		intervals[i][0] = atoi(argv[i * 2 + 1]);
		intervals[i][1] = atoi(argv[i * 2 + 2]);
	}
	int *col_sizes;
	int **results = merge(intervals, (argc - 1) / 2, sizes, &count, &col_sizes);
	for (i = 0; i < count; i++)
	{
		printf("[%d,%d]\n", results[i][0], results[i][1]);
	}
	return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int compare(const void *a, const void *b)
{
	return ((int *)a)[0] - ((int *)b)[0];
}
int **merge(int **intervals, int intervalsSize, int *intervalsColSize, int *returnSize, int **returnColumnSizes)
{
	if (intervalsSize == 0)
	{
		*returnSize = 0;
		return intervals;
	}
	int i, len = 0;
	int *tmp = malloc(intervalsSize * 2 * sizeof(int));
	for (i = 0; i < intervalsSize; i++)
	{
		tmp[i * 2] = intervals[i][0];
		tmp[i * 2 + 1] = intervals[i][1];
	}
	qsort(tmp, intervalsSize, 2 * sizeof(int), compare);
	intervals[0][0] = tmp[0];
	intervals[0][1] = tmp[1];
	for (i = 1; i < intervalsSize; i++)
	{
		if (tmp[i * 2] > intervals[len][1])
		{
			len++;
			intervals[len][0] = tmp[i * 2];
			intervals[len][1] = tmp[i * 2 + 1];
		}
		else if (tmp[i * 2 + 1] > intervals[len][1])
		{
			intervals[len][1] = tmp[i * 2 + 1];
		}
	}
	len += 1;
	*returnSize = len;
	*returnColumnSizes = malloc(len * sizeof(int));
	for (i = 0; i < len; i++)
	{
		(*returnColumnSizes)[i] = 2;
	}
	return intervals;
}
int main(int argc, char **argv)
{
	if (argc < 1 || argc % 2 == 0)
	{
		fprintf(stderr, "Usage: ./test s0 e0 s1 e1...");
		exit(-1);
	}
	int i, count = 0;
	int *sizes = malloc((argc - 1) / 2 * sizeof(int));
	int **intervals = malloc((argc - 1) / 2 * sizeof(int *));
	for (i = 0; i < (argc - 1) / 2; i++)
	{
		sizes[i] = 2;
		intervals[i] = malloc(2 * sizeof(int));
		intervals[i][0] = atoi(argv[i * 2 + 1]);
		intervals[i][1] = atoi(argv[i * 2 + 2]);
	}
	int *col_sizes;
	int **results = merge(intervals, (argc - 1) / 2, sizes, &count, &col_sizes);
	for (i = 0; i < count; i++)
	{
		printf("[%d,%d]\n", results[i][0], results[i][1]);
	}
	return 0;
}
```

## 答案

```cpp
intervals[len][0] = tmp[i * 2];
intervals[len][1] = tmp[i * 2 + 1];
```

## 选项

### A

```cpp
intervals[len][0] = tmp[i];
intervals[len][1] = tmp[i + 1];
```

### B

```cpp
intervals[len][0] = tmp[i];
intervals[len][1] = tmp[i - 1];
```

### C

```cpp
intervals[len][0] = tmp[i * 2];
intervals[len][1] = tmp[i * 2 - 1];
```