Skip to content

Commit 3340e31

Browse files
committed
0014 and 0015 done
1 parent 0eaa34a commit 3340e31

File tree

6 files changed

+217
-0
lines changed

6 files changed

+217
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,26 @@ def longest_common_prefix(strs)
251251
end
252252
```
253253

254+
#### C
255+
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+
}
267+
}
268+
}
269+
return strs[0];
270+
}
271+
272+
```
273+
254274
<!-- tabs:end -->
255275
256276
<!-- solution:end -->

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,26 @@ def longest_common_prefix(strs)
250250
end
251251
```
252252

253+
#### C
254+
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+
}
266+
}
267+
}
268+
return strs[0];
269+
}
270+
271+
```
272+
253273
<!-- tabs:end -->
254274
255275
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
char* longestCommonPrefix(char** strs, int strsSize) {
2+
if (strsSize == 0)
3+
return "";
4+
for (int i = 0; strs[0][i]; i++) {
5+
for (int j = 1; j < strsSize; j++) {
6+
if (strs[j][i] != strs[0][i]) {
7+
strs[0][i] = '\0';
8+
return strs[0];
9+
}
10+
}
11+
}
12+
return strs[0];
13+
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,63 @@ 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+
}
506+
507+
*returnColumnSizes = colSizes;
508+
return result;
509+
}
510+
511+
```
512+
456513
<!-- tabs:end -->
457514
458515
<!-- solution:end -->

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,63 @@ class Solution {
449449
}
450450
```
451451

452+
#### C
453+
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+
}
485+
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+
}
502+
503+
*returnColumnSizes = colSizes;
504+
return result;
505+
}
506+
507+
```
508+
452509
<!-- tabs:end -->
453510
454511
<!-- solution:end -->
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
int cmp(const void* a, const void* b) { return *(int*) a - *(int*) b; }
2+
3+
int** threeSum(int* nums, int numsSize, int* returnSize,
4+
int** returnColumnSizes) {
5+
*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);
10+
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;
16+
17+
int j = i + 1, k = numsSize - 1;
18+
while (j < k) {
19+
int sum = nums[i] + nums[j] + nums[k];
20+
if (sum < 0) {
21+
j++;
22+
} else if (sum > 0) {
23+
k--;
24+
} else {
25+
if (*returnSize >= capacity) {
26+
capacity *= 2;
27+
result = realloc(result, capacity * sizeof(int*));
28+
colSizes = realloc(colSizes, capacity * sizeof(int));
29+
}
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;
36+
(*returnSize)++;
37+
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--;
44+
}
45+
}
46+
}
47+
48+
*returnColumnSizes = colSizes;
49+
return result;
50+
}

0 commit comments

Comments
 (0)