Skip to content

Commit 71d24d7

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent a83a9ec commit 71d24d7

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

001_two_sum/two_sum.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static int compare(const void *a, const void *b)
1111
return ((struct object *) a)->val - ((struct object *) b)->val;
1212
}
1313

14-
static int * twosum(int *nums, int numsSize, int target)
14+
static int * twosum(int *nums, int numsSize, int target, int *returnSize)
1515
{
1616
int i, j;
1717
struct object *objs = malloc(numsSize * sizeof(*objs));
@@ -21,19 +21,19 @@ static int * twosum(int *nums, int numsSize, int target)
2121
}
2222
qsort(objs, numsSize, sizeof(*objs), compare);
2323

24-
int count = 0;
2524
int *results = malloc(2 * sizeof(int));
2625
i = 0;
2726
j = numsSize - 1;
2827
while (i < j) {
29-
int diff = target - objs[i].val;
30-
if (diff > objs[j].val) {
31-
while (++i < j && objs[i].val == objs[i - 1].val) {}
32-
} else if (diff < objs[j].val) {
33-
while (--j > i && objs[j].val == objs[j + 1].val) {}
28+
int sum = objs[i].val + objs[j].val;
29+
if (sum < target) {
30+
i++;
31+
} else if (sum > target) {
32+
j--;
3433
} else {
3534
results[0] = objs[i].index;
3635
results[1] = objs[j].index;
36+
*returnSize = 2;
3737
return results;
3838
}
3939
}

015_three_sum/three_sum.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ static int compare(const void *a, const void *b)
99
static void two_sum(int *nums, int low, int high, int target, int **results, int *count)
1010
{
1111
while (low < high) {
12-
int diff = target - nums[low];
13-
if (diff > nums[high]) {
14-
while (++low < high && nums[low] == nums[low - 1]) {}
15-
} else if (diff < nums[high]) {
16-
while (--high > low && nums[high] == nums[high + 1]) {}
12+
int sum = nums[low] + nums[high];
13+
if (sum < target) {
14+
low++;
15+
} else if (sum > target) {
16+
high--;
1717
} else {
1818
results[*count] = malloc(3 * sizeof(int));
1919
results[*count][0] = -target;

018_four_sum/four_sum.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@ static int compare(const void *a, const void *b)
77
return *(int *) a - *(int *) b;
88
}
99

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)
1212
{
1313
int i;
1414
if (k == 2) {
1515
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--;
2121
} else {
2222
stack[len++] = nums[low];
2323
stack[len++] = nums[high];
2424
results[*count] = malloc(total * sizeof(int));
2525
memcpy(results[*count], stack, total * sizeof(int));
26+
columnSizes[*count] = total;
2627
(*count)++;
2728
len -= 2;
2829
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,
3435
for (i = low; i <= high - k + 1; i++) {
3536
if (i > low && nums[i] == nums[i - 1]) continue;
3637
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);
3839
len--;
3940
}
4041
}
4142
}
4243

4344
/**
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) {
5550
*returnSize = 0;
5651
int i, j, capacity = 50000;
5752
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+
}
6060
return results;
6161
}
6262

030_substring_with_concatenation_of_all_words/concatenation.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ static int *findSubstring(char *s, char **words, int wordsSize, int *returnSize)
128128
for (i = 0; s[i + length] != '\0'; i++) {
129129
memset(fqs, 0, wordsSize * sizeof(int));
130130
for (j = 0; j < wordsSize; j++) {
131+
/* concatenation */
131132
int index = indexes[i + j * len];
132133
if (index < 0 || ++fqs[index] > freqs[index]) {
133134
break;

0 commit comments

Comments
 (0)