Skip to content

Commit 45f4385

Browse files
committed
2 parents c10dfbf + 5bfac18 commit 45f4385

File tree

139 files changed

+36784
-31094
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+36784
-31094
lines changed

images/starcharts.svg

Lines changed: 30737 additions & 30637 deletions
Loading

solution/0000-0099/0014.Longest Common Prefix/README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -253,22 +253,18 @@ end
253253

254254
#### C
255255

256-
``` C
257-
258-
char *longestCommonPrefix(char **strs, int strsSize) {
259-
if (strsSize == 0)
260-
return "";
261-
for (int i = 0; strs[0][i]; i++) {
262-
for (int j = 1; j < strsSize; j++) {
263-
if (strs[j][i] != strs[0][i]) {
264-
strs[0][i] = '\0';
265-
return strs[0];
266-
}
256+
```c
257+
char* longestCommonPrefix(char** strs, int strsSize) {
258+
for (int i = 0; strs[0][i]; i++) {
259+
for (int j = 1; j < strsSize; j++) {
260+
if (strs[j][i] != strs[0][i]) {
261+
strs[0][i] = '\0';
262+
return strs[0];
263+
}
264+
}
267265
}
268-
}
269-
return strs[0];
266+
return strs[0];
270267
}
271-
272268
```
273269
274270
<!-- tabs:end -->

solution/0000-0099/0014.Longest Common Prefix/README_EN.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -252,22 +252,18 @@ end
252252

253253
#### C
254254

255-
``` C
256-
257-
char *longestCommonPrefix(char **strs, int strsSize) {
258-
if (strsSize == 0)
259-
return "";
260-
for (int i = 0; strs[0][i]; i++) {
261-
for (int j = 1; j < strsSize; j++) {
262-
if (strs[j][i] != strs[0][i]) {
263-
strs[0][i] = '\0';
264-
return strs[0];
265-
}
255+
```c
256+
char* longestCommonPrefix(char** strs, int strsSize) {
257+
for (int i = 0; strs[0][i]; i++) {
258+
for (int j = 1; j < strsSize; j++) {
259+
if (strs[j][i] != strs[0][i]) {
260+
strs[0][i] = '\0';
261+
return strs[0];
262+
}
263+
}
266264
}
267-
}
268-
return strs[0];
265+
return strs[0];
269266
}
270-
271267
```
272268
273269
<!-- tabs:end -->

solution/0000-0099/0014.Longest Common Prefix/Solution.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
char* longestCommonPrefix(char** strs, int strsSize) {
2-
if (strsSize == 0)
3-
return "";
42
for (int i = 0; strs[0][i]; i++) {
53
for (int j = 1; j < strsSize; j++) {
64
if (strs[j][i] != strs[0][i]) {

solution/0000-0099/0015.3Sum/README.md

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -453,61 +453,52 @@ class Solution {
453453
}
454454
```
455455

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-
}
456+
#### C
506457

507-
*returnColumnSizes = colSizes;
508-
return result;
458+
```c
459+
int cmp(const void* a, const void* b) {
460+
return *(int*) a - *(int*) b;
509461
}
510462

463+
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
464+
*returnSize = 0;
465+
int cap = 1000;
466+
int** ans = (int**) malloc(sizeof(int*) * cap);
467+
*returnColumnSizes = (int*) malloc(sizeof(int) * cap);
468+
469+
qsort(nums, numsSize, sizeof(int), cmp);
470+
471+
for (int i = 0; i < numsSize - 2 && nums[i] <= 0; ++i) {
472+
if (i > 0 && nums[i] == nums[i - 1]) continue;
473+
int j = i + 1, k = numsSize - 1;
474+
while (j < k) {
475+
int sum = nums[i] + nums[j] + nums[k];
476+
if (sum < 0) {
477+
++j;
478+
} else if (sum > 0) {
479+
--k;
480+
} else {
481+
if (*returnSize >= cap) {
482+
cap *= 2;
483+
ans = (int**) realloc(ans, sizeof(int*) * cap);
484+
*returnColumnSizes = (int*) realloc(*returnColumnSizes, sizeof(int) * cap);
485+
}
486+
ans[*returnSize] = (int*) malloc(sizeof(int) * 3);
487+
ans[*returnSize][0] = nums[i];
488+
ans[*returnSize][1] = nums[j];
489+
ans[*returnSize][2] = nums[k];
490+
(*returnColumnSizes)[*returnSize] = 3;
491+
(*returnSize)++;
492+
493+
++j;
494+
--k;
495+
while (j < k && nums[j] == nums[j - 1]) ++j;
496+
while (j < k && nums[k] == nums[k + 1]) --k;
497+
}
498+
}
499+
}
500+
return ans;
501+
}
511502
```
512503
513504
<!-- tabs:end -->

solution/0000-0099/0015.3Sum/README_EN.md

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -451,59 +451,50 @@ class Solution {
451451

452452
#### C
453453

454-
``` C
455-
456-
int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }
457-
458-
int **threeSum(int *nums, int numsSize, int *returnSize,
459-
int **returnColumnSizes) {
460-
*returnSize = 0;
461-
int capacity = 100;
462-
int **result = malloc(capacity * sizeof(int *));
463-
int *colSizes = malloc(capacity * sizeof(int));
464-
qsort(nums, numsSize, sizeof(int), cmp);
465-
466-
for (int i = 0; i < numsSize - 2; i++) {
467-
if (nums[i] > 0)
468-
break;
469-
if (i > 0 && nums[i] == nums[i - 1])
470-
continue;
471-
472-
int j = i + 1, k = numsSize - 1;
473-
while (j < k) {
474-
int sum = nums[i] + nums[j] + nums[k];
475-
if (sum < 0) {
476-
j++;
477-
} else if (sum > 0) {
478-
k--;
479-
} else {
480-
if (*returnSize >= capacity) {
481-
capacity *= 2;
482-
result = realloc(result, capacity * sizeof(int *));
483-
colSizes = realloc(colSizes, capacity * sizeof(int));
484-
}
454+
```c
455+
int cmp(const void* a, const void* b) {
456+
return *(int*) a - *(int*) b;
457+
}
485458

486-
result[*returnSize] = malloc(3 * sizeof(int));
487-
result[*returnSize][0] = nums[i];
488-
result[*returnSize][1] = nums[j];
489-
result[*returnSize][2] = nums[k];
490-
colSizes[*returnSize] = 3;
491-
(*returnSize)++;
492-
493-
j++;
494-
k--;
495-
while (j < k && nums[j] == nums[j - 1])
496-
j++;
497-
while (j < k && nums[k] == nums[k + 1])
498-
k--;
499-
}
500-
}
501-
}
459+
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
460+
*returnSize = 0;
461+
int cap = 1000;
462+
int** ans = (int**) malloc(sizeof(int*) * cap);
463+
*returnColumnSizes = (int*) malloc(sizeof(int) * cap);
502464

503-
*returnColumnSizes = colSizes;
504-
return result;
505-
}
465+
qsort(nums, numsSize, sizeof(int), cmp);
506466

467+
for (int i = 0; i < numsSize - 2 && nums[i] <= 0; ++i) {
468+
if (i > 0 && nums[i] == nums[i - 1]) continue;
469+
int j = i + 1, k = numsSize - 1;
470+
while (j < k) {
471+
int sum = nums[i] + nums[j] + nums[k];
472+
if (sum < 0) {
473+
++j;
474+
} else if (sum > 0) {
475+
--k;
476+
} else {
477+
if (*returnSize >= cap) {
478+
cap *= 2;
479+
ans = (int**) realloc(ans, sizeof(int*) * cap);
480+
*returnColumnSizes = (int*) realloc(*returnColumnSizes, sizeof(int) * cap);
481+
}
482+
ans[*returnSize] = (int*) malloc(sizeof(int) * 3);
483+
ans[*returnSize][0] = nums[i];
484+
ans[*returnSize][1] = nums[j];
485+
ans[*returnSize][2] = nums[k];
486+
(*returnColumnSizes)[*returnSize] = 3;
487+
(*returnSize)++;
488+
489+
++j;
490+
--k;
491+
while (j < k && nums[j] == nums[j - 1]) ++j;
492+
while (j < k && nums[k] == nums[k + 1]) --k;
493+
}
494+
}
495+
}
496+
return ans;
497+
}
507498
```
508499
509500
<!-- tabs:end -->
Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,43 @@
1-
int cmp(const void* a, const void* b) { return *(int*) a - *(int*) b; }
1+
int cmp(const void* a, const void* b) {
2+
return *(int*) a - *(int*) b;
3+
}
24

3-
int** threeSum(int* nums, int numsSize, int* returnSize,
4-
int** returnColumnSizes) {
5+
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
56
*returnSize = 0;
6-
int capacity = 100;
7-
int** result = malloc(capacity * sizeof(int*));
8-
int* colSizes = malloc(capacity * sizeof(int));
9-
qsort(nums, numsSize, sizeof(int), cmp);
7+
int cap = 1000;
8+
int** ans = (int**) malloc(sizeof(int*) * cap);
9+
*returnColumnSizes = (int*) malloc(sizeof(int) * cap);
1010

11-
for (int i = 0; i < numsSize - 2; i++) {
12-
if (nums[i] > 0)
13-
break;
14-
if (i > 0 && nums[i] == nums[i - 1])
15-
continue;
11+
qsort(nums, numsSize, sizeof(int), cmp);
1612

13+
for (int i = 0; i < numsSize - 2 && nums[i] <= 0; ++i) {
14+
if (i > 0 && nums[i] == nums[i - 1]) continue;
1715
int j = i + 1, k = numsSize - 1;
1816
while (j < k) {
1917
int sum = nums[i] + nums[j] + nums[k];
2018
if (sum < 0) {
21-
j++;
19+
++j;
2220
} else if (sum > 0) {
23-
k--;
21+
--k;
2422
} else {
25-
if (*returnSize >= capacity) {
26-
capacity *= 2;
27-
result = realloc(result, capacity * sizeof(int*));
28-
colSizes = realloc(colSizes, capacity * sizeof(int));
23+
if (*returnSize >= cap) {
24+
cap *= 2;
25+
ans = (int**) realloc(ans, sizeof(int*) * cap);
26+
*returnColumnSizes = (int*) realloc(*returnColumnSizes, sizeof(int) * cap);
2927
}
30-
31-
result[*returnSize] = malloc(3 * sizeof(int));
32-
result[*returnSize][0] = nums[i];
33-
result[*returnSize][1] = nums[j];
34-
result[*returnSize][2] = nums[k];
35-
colSizes[*returnSize] = 3;
28+
ans[*returnSize] = (int*) malloc(sizeof(int) * 3);
29+
ans[*returnSize][0] = nums[i];
30+
ans[*returnSize][1] = nums[j];
31+
ans[*returnSize][2] = nums[k];
32+
(*returnColumnSizes)[*returnSize] = 3;
3633
(*returnSize)++;
3734

38-
j++;
39-
k--;
40-
while (j < k && nums[j] == nums[j - 1])
41-
j++;
42-
while (j < k && nums[k] == nums[k + 1])
43-
k--;
35+
++j;
36+
--k;
37+
while (j < k && nums[j] == nums[j - 1]) ++j;
38+
while (j < k && nums[k] == nums[k + 1]) --k;
4439
}
4540
}
4641
}
47-
48-
*returnColumnSizes = colSizes;
49-
return result;
42+
return ans;
5043
}

0 commit comments

Comments
 (0)