Skip to content

Commit e63bbb4

Browse files
feat: add c solutions to lc problems: No.0016,0017 (#4544)
1 parent db8cc79 commit e63bbb4

File tree

6 files changed

+209
-0
lines changed

6 files changed

+209
-0
lines changed

solution/0000-0099/0016.3Sum Closest/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,37 @@ class Solution {
315315
}
316316
```
317317

318+
#### C
319+
320+
```c
321+
int cmp(const void* a, const void* b) {
322+
return (*(int*) a - *(int*) b);
323+
}
324+
325+
int threeSumClosest(int* nums, int numsSize, int target) {
326+
qsort(nums, numsSize, sizeof(int), cmp);
327+
int ans = 1 << 30;
328+
for (int i = 0; i < numsSize; ++i) {
329+
int j = i + 1, k = numsSize - 1;
330+
while (j < k) {
331+
int t = nums[i] + nums[j] + nums[k];
332+
if (t == target) {
333+
return t;
334+
}
335+
if (abs(t - target) < abs(ans - target)) {
336+
ans = t;
337+
}
338+
if (t > target) {
339+
--k;
340+
} else {
341+
++j;
342+
}
343+
}
344+
}
345+
return ans;
346+
}
347+
```
348+
318349
<!-- tabs:end -->
319350
320351
<!-- solution:end -->

solution/0000-0099/0016.3Sum Closest/README_EN.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,37 @@ class Solution {
314314
}
315315
```
316316

317+
#### C
318+
319+
```c
320+
int cmp(const void* a, const void* b) {
321+
return (*(int*) a - *(int*) b);
322+
}
323+
324+
int threeSumClosest(int* nums, int numsSize, int target) {
325+
qsort(nums, numsSize, sizeof(int), cmp);
326+
int ans = 1 << 30;
327+
for (int i = 0; i < numsSize; ++i) {
328+
int j = i + 1, k = numsSize - 1;
329+
while (j < k) {
330+
int t = nums[i] + nums[j] + nums[k];
331+
if (t == target) {
332+
return t;
333+
}
334+
if (abs(t - target) < abs(ans - target)) {
335+
ans = t;
336+
}
337+
if (t > target) {
338+
--k;
339+
} else {
340+
++j;
341+
}
342+
}
343+
}
344+
return ans;
345+
}
346+
```
347+
317348
<!-- tabs:end -->
318349
319350
<!-- solution:end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
int cmp(const void* a, const void* b) {
2+
return (*(int*) a - *(int*) b);
3+
}
4+
5+
int threeSumClosest(int* nums, int numsSize, int target) {
6+
qsort(nums, numsSize, sizeof(int), cmp);
7+
int ans = 1 << 30;
8+
for (int i = 0; i < numsSize; ++i) {
9+
int j = i + 1, k = numsSize - 1;
10+
while (j < k) {
11+
int t = nums[i] + nums[j] + nums[k];
12+
if (t == target) {
13+
return t;
14+
}
15+
if (abs(t - target) < abs(ans - target)) {
16+
ans = t;
17+
}
18+
if (t > target) {
19+
--k;
20+
} else {
21+
++j;
22+
}
23+
}
24+
}
25+
return ans;
26+
}

solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,48 @@ class Solution {
555555
}
556556
```
557557

558+
#### C
559+
560+
```c
561+
char* d[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
562+
563+
char** letterCombinations(char* digits, int* returnSize) {
564+
if (!*digits) {
565+
*returnSize = 0;
566+
return NULL;
567+
}
568+
569+
int size = 1;
570+
char** ans = (char**) malloc(sizeof(char*));
571+
ans[0] = strdup("");
572+
573+
for (int x = 0; digits[x]; ++x) {
574+
char* s = d[digits[x] - '2'];
575+
int len = strlen(s);
576+
char** t = (char**) malloc(sizeof(char*) * size * len);
577+
int tSize = 0;
578+
579+
for (int i = 0; i < size; ++i) {
580+
for (int j = 0; j < len; ++j) {
581+
int oldLen = strlen(ans[i]);
582+
char* tmp = (char*) malloc(oldLen + 2);
583+
strcpy(tmp, ans[i]);
584+
tmp[oldLen] = s[j];
585+
tmp[oldLen + 1] = '\0';
586+
t[tSize++] = tmp;
587+
}
588+
free(ans[i]);
589+
}
590+
free(ans);
591+
ans = t;
592+
size = tSize;
593+
}
594+
595+
*returnSize = size;
596+
return ans;
597+
}
598+
```
599+
558600
<!-- tabs:end -->
559601

560602
<!-- solution:end -->

solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,48 @@ class Solution {
551551
}
552552
```
553553

554+
#### C
555+
556+
```c
557+
char* d[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
558+
559+
char** letterCombinations(char* digits, int* returnSize) {
560+
if (!*digits) {
561+
*returnSize = 0;
562+
return NULL;
563+
}
564+
565+
int size = 1;
566+
char** ans = (char**) malloc(sizeof(char*));
567+
ans[0] = strdup("");
568+
569+
for (int x = 0; digits[x]; ++x) {
570+
char* s = d[digits[x] - '2'];
571+
int len = strlen(s);
572+
char** t = (char**) malloc(sizeof(char*) * size * len);
573+
int tSize = 0;
574+
575+
for (int i = 0; i < size; ++i) {
576+
for (int j = 0; j < len; ++j) {
577+
int oldLen = strlen(ans[i]);
578+
char* tmp = (char*) malloc(oldLen + 2);
579+
strcpy(tmp, ans[i]);
580+
tmp[oldLen] = s[j];
581+
tmp[oldLen + 1] = '\0';
582+
t[tSize++] = tmp;
583+
}
584+
free(ans[i]);
585+
}
586+
free(ans);
587+
ans = t;
588+
size = tSize;
589+
}
590+
591+
*returnSize = size;
592+
return ans;
593+
}
594+
```
595+
554596
<!-- tabs:end -->
555597

556598
<!-- solution:end -->
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
char* d[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
2+
3+
char** letterCombinations(char* digits, int* returnSize) {
4+
if (!*digits) {
5+
*returnSize = 0;
6+
return NULL;
7+
}
8+
9+
int size = 1;
10+
char** ans = (char**) malloc(sizeof(char*));
11+
ans[0] = strdup("");
12+
13+
for (int x = 0; digits[x]; ++x) {
14+
char* s = d[digits[x] - '2'];
15+
int len = strlen(s);
16+
char** t = (char**) malloc(sizeof(char*) * size * len);
17+
int tSize = 0;
18+
19+
for (int i = 0; i < size; ++i) {
20+
for (int j = 0; j < len; ++j) {
21+
int oldLen = strlen(ans[i]);
22+
char* tmp = (char*) malloc(oldLen + 2);
23+
strcpy(tmp, ans[i]);
24+
tmp[oldLen] = s[j];
25+
tmp[oldLen + 1] = '\0';
26+
t[tSize++] = tmp;
27+
}
28+
free(ans[i]);
29+
}
30+
free(ans);
31+
ans = t;
32+
size = tSize;
33+
}
34+
35+
*returnSize = size;
36+
return ans;
37+
}

0 commit comments

Comments
 (0)