@@ -336,7 +336,49 @@ impl Solution {
336
336
}
337
337
}
338
338
```
339
+ ### C
340
+
341
+ ``` c
342
+ #define max (a, b ) ((a) > (b) ? (a) : (b))
343
+
344
+ // 根据左边界进行排序
345
+ int cmp (const void * var1, const void * var2){
346
+ int * v1 = * (int ** ) var1;
347
+ int * v2 = * (int ** ) var2;
348
+ return v1[ 0] - v2[ 0] ;
349
+ }
350
+
351
+ int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes) {
352
+ int ** result = malloc(sizeof (int * ) * intervalsSize);
353
+ * returnColumnSizes = malloc(sizeof (int ) * intervalsSize);
354
+ for(int i = 0; i < intervalsSize; i++){
355
+ result[ i] = malloc(sizeof (int ) * 2);
356
+ }
357
+ qsort(intervals, intervalsSize, sizeof (int * ), cmp);
358
+ int count = 0;
359
+ for(int i = 0; i < intervalsSize; i++){
360
+ // 记录区间的左右边界
361
+ int L = intervals[ i] [ 0 ] , R = intervals[ i] [ 1 ] ;
362
+ // 如果count为0或者前一区间的右区间小于此时的左边,加入结果中
363
+ if (count == 0 || result[ count - 1] [ 1 ] < L) {
364
+ returnColumnSizes[ 0] [ count ] = 2;
365
+ result[ count] [ 0 ] = L;
366
+ result[ count] [ 1 ] = R;
367
+ count++;
368
+ }
369
+ else{ // 更新右边界的值
370
+ result[ count - 1] [ 1 ] = max(R, result[ count - 1] [ 1 ] );
371
+ }
372
+ }
373
+ * returnSize = count;
374
+ return result;
375
+ }
376
+ ```
377
+
378
+
379
+
339
380
### C#
381
+
340
382
```csharp
341
383
public class Solution
342
384
{
@@ -367,4 +409,3 @@ public class Solution
367
409
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
368
410
<img src =" ../pics/网站星球宣传海报.jpg " width =" 1000 " />
369
411
</a >
370
-
0 commit comments