@@ -7,22 +7,23 @@ static int compare(const void *a, const void *b)
7
7
return * (int * ) a - * (int * ) b ;
8
8
}
9
9
10
- static void k_sum (int * nums , int low , int high , int target , int total ,
11
- int k , int * stack , int len , int * * results , int * count )
10
+ static void k_sum (int * nums , int low , int high , int target , int total , int k ,
11
+ int * stack , int len , int * * results , int * count , int * columnSizes )
12
12
{
13
13
int i ;
14
14
if (k == 2 ) {
15
15
while (low < high ) {
16
- int diff = target - nums [low ];
17
- if (diff > nums [ high ] ) {
18
- while ( ++ low < high && nums [ low ] == nums [ low - 1 ]) {}
19
- } else if (diff < nums [ high ] ) {
20
- while ( -- high > low && nums [ high ] == nums [ high + 1 ]) {}
16
+ int sum = nums [ low ] + nums [high ];
17
+ if (sum < target ) {
18
+ low ++ ;
19
+ } else if (sum > target ) {
20
+ high -- ;
21
21
} else {
22
22
stack [len ++ ] = nums [low ];
23
23
stack [len ++ ] = nums [high ];
24
24
results [* count ] = malloc (total * sizeof (int ));
25
25
memcpy (results [* count ], stack , total * sizeof (int ));
26
+ columnSizes [* count ] = total ;
26
27
(* count )++ ;
27
28
len -= 2 ;
28
29
while (++ low < high && nums [low ] == nums [low - 1 ]) {}
@@ -34,29 +35,28 @@ static void k_sum(int *nums, int low, int high, int target, int total,
34
35
for (i = low ; i <= high - k + 1 ; i ++ ) {
35
36
if (i > low && nums [i ] == nums [i - 1 ]) continue ;
36
37
stack [len ++ ] = nums [i ];
37
- k_sum (nums , i + 1 , high , target - nums [i ], 4 , k - 1 , stack , len , results , count );
38
+ k_sum (nums , i + 1 , high , target - nums [i ], 4 , k - 1 , stack , len , results , count , columnSizes );
38
39
len -- ;
39
40
}
40
41
}
41
42
}
42
43
43
44
/**
44
- ** Return an array of arrays of size *returnSize.
45
- ** Note: The returned array must be malloced, assume caller calls free().
46
- **/
47
- static int * * fourSum (int * nums , int numsSize , int target , int * returnSize )
48
- {
49
- if (numsSize < 4 ) {
50
- return NULL ;
51
- }
52
-
53
- qsort (nums , numsSize , sizeof (* nums ), compare );
54
-
45
+ * Return an array of arrays of size *returnSize.
46
+ * The sizes of the arrays are returned as *returnColumnSizes array.
47
+ * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
48
+ */
49
+ int * * fourSum (int * nums , int numsSize , int target , int * returnSize , int * * returnColumnSizes ) {
55
50
* returnSize = 0 ;
56
51
int i , j , capacity = 50000 ;
57
52
int * * results = malloc (capacity * sizeof (int * ));
58
- int * stack = malloc (4 * sizeof (int ));
59
- k_sum (nums , 0 , numsSize - 1 , target , 4 , 4 , stack , 0 , results , returnSize );
53
+ * returnColumnSizes = malloc (capacity * sizeof (int ));
54
+
55
+ if (numsSize >= 4 ) {
56
+ qsort (nums , numsSize , sizeof (* nums ), compare );
57
+ int * stack = malloc (4 * sizeof (int ));
58
+ k_sum (nums , 0 , numsSize - 1 , target , 4 , 4 , stack , 0 , results , returnSize , * returnColumnSizes );
59
+ }
60
60
return results ;
61
61
}
62
62
0 commit comments