diff --git a/solution/0000-0099/0016.3Sum Closest/README.md b/solution/0000-0099/0016.3Sum Closest/README.md index 406e23f70dbb9..c724790896aff 100644 --- a/solution/0000-0099/0016.3Sum Closest/README.md +++ b/solution/0000-0099/0016.3Sum Closest/README.md @@ -315,6 +315,37 @@ class Solution { } ``` +#### C + +```c +int cmp(const void* a, const void* b) { + return (*(int*) a - *(int*) b); +} + +int threeSumClosest(int* nums, int numsSize, int target) { + qsort(nums, numsSize, sizeof(int), cmp); + int ans = 1 << 30; + for (int i = 0; i < numsSize; ++i) { + int j = i + 1, k = numsSize - 1; + while (j < k) { + int t = nums[i] + nums[j] + nums[k]; + if (t == target) { + return t; + } + if (abs(t - target) < abs(ans - target)) { + ans = t; + } + if (t > target) { + --k; + } else { + ++j; + } + } + } + return ans; +} +``` + diff --git a/solution/0000-0099/0016.3Sum Closest/README_EN.md b/solution/0000-0099/0016.3Sum Closest/README_EN.md index b5ccdf0198cd0..3366410926aee 100644 --- a/solution/0000-0099/0016.3Sum Closest/README_EN.md +++ b/solution/0000-0099/0016.3Sum Closest/README_EN.md @@ -314,6 +314,37 @@ class Solution { } ``` +#### C + +```c +int cmp(const void* a, const void* b) { + return (*(int*) a - *(int*) b); +} + +int threeSumClosest(int* nums, int numsSize, int target) { + qsort(nums, numsSize, sizeof(int), cmp); + int ans = 1 << 30; + for (int i = 0; i < numsSize; ++i) { + int j = i + 1, k = numsSize - 1; + while (j < k) { + int t = nums[i] + nums[j] + nums[k]; + if (t == target) { + return t; + } + if (abs(t - target) < abs(ans - target)) { + ans = t; + } + if (t > target) { + --k; + } else { + ++j; + } + } + } + return ans; +} +``` + diff --git a/solution/0000-0099/0016.3Sum Closest/Solution.c b/solution/0000-0099/0016.3Sum Closest/Solution.c new file mode 100644 index 0000000000000..778c1a2aca594 --- /dev/null +++ b/solution/0000-0099/0016.3Sum Closest/Solution.c @@ -0,0 +1,26 @@ +int cmp(const void* a, const void* b) { + return (*(int*) a - *(int*) b); +} + +int threeSumClosest(int* nums, int numsSize, int target) { + qsort(nums, numsSize, sizeof(int), cmp); + int ans = 1 << 30; + for (int i = 0; i < numsSize; ++i) { + int j = i + 1, k = numsSize - 1; + while (j < k) { + int t = nums[i] + nums[j] + nums[k]; + if (t == target) { + return t; + } + if (abs(t - target) < abs(ans - target)) { + ans = t; + } + if (t > target) { + --k; + } else { + ++j; + } + } + } + return ans; +} diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md index a8ca434436f2e..cd8e10eceb70c 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md @@ -555,6 +555,48 @@ class Solution { } ``` +#### C + +```c +char* d[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + +char** letterCombinations(char* digits, int* returnSize) { + if (!*digits) { + *returnSize = 0; + return NULL; + } + + int size = 1; + char** ans = (char**) malloc(sizeof(char*)); + ans[0] = strdup(""); + + for (int x = 0; digits[x]; ++x) { + char* s = d[digits[x] - '2']; + int len = strlen(s); + char** t = (char**) malloc(sizeof(char*) * size * len); + int tSize = 0; + + for (int i = 0; i < size; ++i) { + for (int j = 0; j < len; ++j) { + int oldLen = strlen(ans[i]); + char* tmp = (char*) malloc(oldLen + 2); + strcpy(tmp, ans[i]); + tmp[oldLen] = s[j]; + tmp[oldLen + 1] = '\0'; + t[tSize++] = tmp; + } + free(ans[i]); + } + free(ans); + ans = t; + size = tSize; + } + + *returnSize = size; + return ans; +} +``` + diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md index ffad07737163e..dba840d7bd8ee 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md @@ -551,6 +551,48 @@ class Solution { } ``` +#### C + +```c +char* d[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + +char** letterCombinations(char* digits, int* returnSize) { + if (!*digits) { + *returnSize = 0; + return NULL; + } + + int size = 1; + char** ans = (char**) malloc(sizeof(char*)); + ans[0] = strdup(""); + + for (int x = 0; digits[x]; ++x) { + char* s = d[digits[x] - '2']; + int len = strlen(s); + char** t = (char**) malloc(sizeof(char*) * size * len); + int tSize = 0; + + for (int i = 0; i < size; ++i) { + for (int j = 0; j < len; ++j) { + int oldLen = strlen(ans[i]); + char* tmp = (char*) malloc(oldLen + 2); + strcpy(tmp, ans[i]); + tmp[oldLen] = s[j]; + tmp[oldLen + 1] = '\0'; + t[tSize++] = tmp; + } + free(ans[i]); + } + free(ans); + ans = t; + size = tSize; + } + + *returnSize = size; + return ans; +} +``` + diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.c b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.c new file mode 100644 index 0000000000000..e02c971a3a8c6 --- /dev/null +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.c @@ -0,0 +1,37 @@ +char* d[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + +char** letterCombinations(char* digits, int* returnSize) { + if (!*digits) { + *returnSize = 0; + return NULL; + } + + int size = 1; + char** ans = (char**) malloc(sizeof(char*)); + ans[0] = strdup(""); + + for (int x = 0; digits[x]; ++x) { + char* s = d[digits[x] - '2']; + int len = strlen(s); + char** t = (char**) malloc(sizeof(char*) * size * len); + int tSize = 0; + + for (int i = 0; i < size; ++i) { + for (int j = 0; j < len; ++j) { + int oldLen = strlen(ans[i]); + char* tmp = (char*) malloc(oldLen + 2); + strcpy(tmp, ans[i]); + tmp[oldLen] = s[j]; + tmp[oldLen + 1] = '\0'; + t[tSize++] = tmp; + } + free(ans[i]); + } + free(ans); + ans = t; + size = tSize; + } + + *returnSize = size; + return ans; +}