From e36c3d56badb897669956071f93d326cc276f02a Mon Sep 17 00:00:00 2001 From: ArthurP Date: Mon, 20 Sep 2021 08:02:20 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200093.=E5=A4=8D?= =?UTF-8?q?=E5=8E=9FIP=E5=9C=B0=E5=9D=80.md=20C=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0093.复原IP地址.md | 76 +++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 9f2ea6e7..404f3a01 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -476,6 +476,82 @@ func isNormalIp(s string,startIndex,end int)bool{ ``` +C: +```c +//记录结果 +char** result; +int resultTop; +//记录应该加入'.'的位置 +int segments[3]; +int isValid(char* s, int start, int end) { + if(start > end) + return 0; + if (s[start] == '0' && start != end) { // 0开头的数字不合法 + return false; + } + int num = 0; + for (int i = start; i <= end; i++) { + if (s[i] > '9' || s[i] < '0') { // 遇到非数字字符不合法 + return false; + } + num = num * 10 + (s[i] - '0'); + if (num > 255) { // 如果大于255了不合法 + return false; + } + } + return true; +} + +//startIndex为起始搜索位置,pointNum为'.'对象 +void backTracking(char* s, int startIndex, int pointNum) { + //若'.'数量为3,分隔结束 + if(pointNum == 3) { + //若最后一段字符串符合要求,将当前的字符串放入result种 + if(isValid(s, startIndex, strlen(s) - 1)) { + char* tempString = (char*)malloc(sizeof(char) * strlen(s) + 4); + int j; + //记录添加字符时tempString的下标 + int count = 0; + //记录添加字符时'.'的使用数量 + int count1 = 0; + for(j = 0; j < strlen(s); j++) { + tempString[count++] = s[j]; + //若'.'的使用数量小于3且当前下标等于'.'下标,添加'.'到数组 + if(count1 < 3 && j == segments[count1]) { + tempString[count++] = '.'; + count1++; + } + } + tempString[count] = 0; + //扩容result数组 + result = (char**)realloc(result, sizeof(char*) * (resultTop + 1)); + result[resultTop++] = tempString; + } + return ; + } + + int i; + for(i = startIndex; i < strlen(s); i++) { + if(isValid(s, startIndex, i)) { + //记录应该添加'.'的位置 + segments[pointNum] = i; + backTracking(s, i + 1, pointNum + 1); + } + else { + break; + } + } +} + +char ** restoreIpAddresses(char * s, int* returnSize){ + result = (char**)malloc(0); + resultTop = 0; + backTracking(s, 0, 0); + *returnSize = resultTop; + return result; +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 6693cad9f6f1c012b93d02c05347b4d46ee132b8 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Wed, 22 Sep 2021 09:34:21 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200078.=E5=AD=90?= =?UTF-8?q?=E9=9B=86.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0078.子集.md | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/problems/0078.子集.md b/problems/0078.子集.md index 583fe664..80eb165d 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -263,6 +263,63 @@ var subsets = function(nums) { }; ``` +C: +```c +int* path; +int pathTop; +int** ans; +int ansTop; +//记录二维数组中每个一维数组的长度 +int* length; +//将当前path数组复制到ans中 +void copy() { + int* tempPath = (int*)malloc(sizeof(int) * pathTop); + int i; + for(i = 0; i < pathTop; i++) { + tempPath[i] = path[i]; + } + ans = (int**)realloc(ans, sizeof(int*) * (ansTop+1)); + length[ansTop] = pathTop; + ans[ansTop++] = tempPath; +} + +void backTracking(int* nums, int numsSize, int startIndex) { + //收集子集,要放在终止添加的上面,否则会漏掉自己 + copy(); + //若startIndex大于数组大小,返回 + if(startIndex >= numsSize) { + return; + } + int j; + for(j = startIndex; j < numsSize; j++) { + //将当前下标数字放入path中 + path[pathTop++] = nums[j]; + backTracking(nums, numsSize, j+1); + //回溯 + pathTop--; + } +} + +int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){ + //初始化辅助变量 + path = (int*)malloc(sizeof(int) * numsSize); + ans = (int**)malloc(0); + length = (int*)malloc(sizeof(int) * 1500); + ansTop = pathTop = 0; + //进入回溯 + backTracking(nums, numsSize, 0); + //设置二维数组中元素个数 + *returnSize = ansTop; + //设置二维数组中每个一维数组的长度 + *returnColumnSizes = (int*)malloc(sizeof(int) * ansTop); + int i; + for(i = 0; i < ansTop; i++) { + (*returnColumnSizes)[i] = length[i]; + } + return ans; +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 5a851142f15179d271d88081d17e41957636c58d Mon Sep 17 00:00:00 2001 From: ArthurP Date: Wed, 22 Sep 2021 09:40:55 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200455.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E9=A5=BC=E5=B9=B2.md=20C=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 8e20c402..bae1566c 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -217,6 +217,29 @@ var findContentChildren = function(g, s) { ``` +C: +```c +int cmp(int* a, int* b) { + return *a - *b; +} + +int findContentChildren(int* g, int gSize, int* s, int sSize){ + if(sSize == 0) + return 0; + + //将两个数组排序为升序 + qsort(g, gSize, sizeof(int), cmp); + qsort(s, sSize, sizeof(int), cmp); + + int numFedChildren = 0; + int i = 0; + for(i = 0; i < sSize; ++i) { + if(numFedChildren < gSize && g[numFedChildren] <= s[i]) + numFedChildren++; + } + return numFedChildren; +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)