@@ -453,6 +453,63 @@ class Solution {
453
453
}
454
454
```
455
455
456
+ #### C
457
+
458
+ ``` C
459
+
460
+ int cmp (const void * a, const void * b) { return * (int * )a - * (int * )b; }
461
+
462
+ int ** threeSum(int * nums, int numsSize, int * returnSize,
463
+ int ** returnColumnSizes) {
464
+ * returnSize = 0;
465
+ int capacity = 100;
466
+ int ** result = malloc(capacity * sizeof(int * ));
467
+ int * colSizes = malloc(capacity * sizeof(int));
468
+ qsort(nums, numsSize, sizeof(int), cmp);
469
+
470
+ for (int i = 0; i < numsSize - 2; i++) {
471
+ if (nums[ i] > 0)
472
+ break;
473
+ if (i > 0 && nums[ i] == nums[ i - 1] )
474
+ continue;
475
+
476
+ int j = i + 1, k = numsSize - 1;
477
+ while (j < k) {
478
+ int sum = nums[i] + nums[j] + nums[k];
479
+ if (sum < 0) {
480
+ j++;
481
+ } else if (sum > 0) {
482
+ k--;
483
+ } else {
484
+ if (*returnSize >= capacity) {
485
+ capacity *= 2;
486
+ result = realloc(result, capacity * sizeof(int *));
487
+ colSizes = realloc(colSizes, capacity * sizeof(int));
488
+ }
489
+
490
+ result[*returnSize] = malloc(3 * sizeof(int));
491
+ result[*returnSize][0] = nums[i];
492
+ result[*returnSize][1] = nums[j];
493
+ result[*returnSize][2] = nums[k];
494
+ colSizes[*returnSize] = 3;
495
+ (*returnSize)++;
496
+
497
+ j++;
498
+ k--;
499
+ while (j < k && nums[j] == nums[j - 1])
500
+ j++;
501
+ while (j < k && nums[k] == nums[k + 1])
502
+ k--;
503
+ }
504
+ }
505
+ }
506
+
507
+ * returnColumnSizes = colSizes;
508
+ return result;
509
+ }
510
+
511
+ ```
512
+
456
513
<!-- tabs:end -->
457
514
458
515
<!-- solution:end -->
0 commit comments