Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 05ffb40

Browse files
feat: add c solutions to lc problems: No.0014,0015 (#4519)
1 parent 18a93fa commit 05ffb40

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

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

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

254+
#### C
255+
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+
}
265+
}
266+
return strs[0];
267+
}
268+
```
269+
254270
<!-- tabs:end -->
255271
256272
<!-- solution:end -->

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

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

253+
#### C
254+
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+
}
264+
}
265+
return strs[0];
266+
}
267+
```
268+
253269
<!-- tabs:end -->
254270
255271
<!-- solution:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
char* longestCommonPrefix(char** strs, int strsSize) {
2+
for (int i = 0; strs[0][i]; i++) {
3+
for (int j = 1; j < strsSize; j++) {
4+
if (strs[j][i] != strs[0][i]) {
5+
strs[0][i] = '0円';
6+
return strs[0];
7+
}
8+
}
9+
}
10+
return strs[0];
11+
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,54 @@ class Solution {
453453
}
454454
```
455455

456+
#### C
457+
458+
```c
459+
int cmp(const void* a, const void* b) {
460+
return *(int*) a - *(int*) b;
461+
}
462+
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+
}
502+
```
503+
456504
<!-- tabs:end -->
457505
458506
<!-- solution:end -->

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

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

452+
#### C
453+
454+
```c
455+
int cmp(const void* a, const void* b) {
456+
return *(int*) a - *(int*) b;
457+
}
458+
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);
464+
465+
qsort(nums, numsSize, sizeof(int), cmp);
466+
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+
}
498+
```
499+
452500
<!-- tabs:end -->
453501
454502
<!-- solution:end -->
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
int cmp(const void* a, const void* b) {
2+
return *(int*) a - *(int*) b;
3+
}
4+
5+
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
6+
*returnSize = 0;
7+
int cap = 1000;
8+
int** ans = (int**) malloc(sizeof(int*) * cap);
9+
*returnColumnSizes = (int*) malloc(sizeof(int) * cap);
10+
11+
qsort(nums, numsSize, sizeof(int), cmp);
12+
13+
for (int i = 0; i < numsSize - 2 && nums[i] <= 0; ++i) {
14+
if (i > 0 && nums[i] == nums[i - 1]) continue;
15+
int j = i + 1, k = numsSize - 1;
16+
while (j < k) {
17+
int sum = nums[i] + nums[j] + nums[k];
18+
if (sum < 0) {
19+
++j;
20+
} else if (sum > 0) {
21+
--k;
22+
} else {
23+
if (*returnSize >= cap) {
24+
cap *= 2;
25+
ans = (int**) realloc(ans, sizeof(int*) * cap);
26+
*returnColumnSizes = (int*) realloc(*returnColumnSizes, sizeof(int) * cap);
27+
}
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;
33+
(*returnSize)++;
34+
35+
++j;
36+
--k;
37+
while (j < k && nums[j] == nums[j - 1]) ++j;
38+
while (j < k && nums[k] == nums[k + 1]) --k;
39+
}
40+
}
41+
}
42+
return ans;
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /