mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0056.合并区间.md
0056.合并区间新增C语言实现
This commit is contained in:
@ -336,7 +336,49 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C
|
||||||
|
|
||||||
|
```c
|
||||||
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||||
|
|
||||||
|
// 根据左边界进行排序
|
||||||
|
int cmp(const void * var1, const void * var2){
|
||||||
|
int *v1 = *(int **) var1;
|
||||||
|
int *v2 = *(int **) var2;
|
||||||
|
return v1[0] - v2[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes) {
|
||||||
|
int ** result = malloc(sizeof (int *) * intervalsSize);
|
||||||
|
* returnColumnSizes = malloc(sizeof (int ) * intervalsSize);
|
||||||
|
for(int i = 0; i < intervalsSize; i++){
|
||||||
|
result[i] = malloc(sizeof (int ) * 2);
|
||||||
|
}
|
||||||
|
qsort(intervals, intervalsSize, sizeof (int *), cmp);
|
||||||
|
int count = 0;
|
||||||
|
for(int i = 0; i < intervalsSize; i++){
|
||||||
|
// 记录区间的左右边界
|
||||||
|
int L = intervals[i][0], R = intervals[i][1];
|
||||||
|
// 如果count为0或者前一区间的右区间小于此时的左边,加入结果中
|
||||||
|
if (count == 0 || result[count - 1][1] < L) {
|
||||||
|
returnColumnSizes[0][count] = 2;
|
||||||
|
result[count][0] = L;
|
||||||
|
result[count][1] = R;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
else{ // 更新右边界的值
|
||||||
|
result[count - 1][1] = max(R, result[count - 1][1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*returnSize = count;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### C#
|
### C#
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public class Solution
|
public class Solution
|
||||||
{
|
{
|
||||||
@ -367,4 +409,3 @@ public class Solution
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user