From 4154d3db2eadecdddaba45d09f09bfe16c0cc594 Mon Sep 17 00:00:00 2001 From: 243wresfdxvc Date: Thu, 21 Apr 2022 09:55:19 +0000 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200134.=20=E5=8A=A0?= =?UTF-8?q?=E6=B2=B9=E7=AB=99.md=20C=E8=AF=AD=E8=A8=80=E8=B4=AA=E5=BF=83?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E6=96=B9=E6=B3=95=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index ca95af67..45e05fed 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -341,6 +341,7 @@ var canCompleteCircuit = function(gas, cost) { ``` ### C +贪心算法:方法一 ```c int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ int curSum = 0; @@ -370,5 +371,36 @@ int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ } ``` +贪心算法:方法二 +```c +int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ + int curSum = 0; + int totalSum = 0; + int start = 0; + + int i; + for(i = 0; i < gasSize; ++i) { + // 当前i站中加油量与耗油量的差 + int diff = gas[i] - cost[i]; + + curSum += diff; + totalSum += diff; + + // 若0到i的加油量都为负,则开始位置应为i+1 + if(curSum < 0) { + curSum = 0; + // 当i + 1 == gasSize时,totalSum < 0(此时i为gasSize - 1),油车不可能返回原点 + start = i + 1; + } + } + + // 若总和小于0,加油车无论如何都无法返回原点。返回-1 + if(totalSum < 0) + return -1; + + return start; +} +``` + -----------------------
From e2807eb59ad5b9e45311544278c905fd942e85b3 Mon Sep 17 00:00:00 2001 From: 243wresfdxvc Date: Sat, 23 Apr 2022 16:15:53 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200203.=E7=A7=BB?= =?UTF-8?q?=E5=87=BA=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0.md=20C=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0203.移除链表元素.md | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index c34831b7..751553e2 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -145,6 +145,38 @@ public: ## 其他语言版本 C: +用原来的链表操作: +```c +struct ListNode* removeElements(struct ListNode* head, int val){ + struct ListNode* temp; + // 当头结点存在并且头结点的值等于val时 + while(head && head->val == val) { + temp = head; + // 将新的头结点设置为head->next并删除原来的头结点 + head = head->next; + free(temp); + } + + struct ListNode *cur = head; + // 当cur存在并且cur->next存在时 + // 此解法需要判断cur存在因为cur指向head。若head本身为NULL或者原链表中元素都为val的话,cur也会为NULL + while(cur && (temp = cur->next)) { + // 若cur->next的值等于val + if(temp->val == val) { + // 将cur->next设置为cur->next->next并删除cur->next + cur->next = temp->next; + free(temp); + } + // 若cur->next不等于val,则将cur后移一位 + else + cur = cur->next; + } + + // 返回头结点 + return head; +} +``` +设置一个虚拟头结点: ```c /** * Definition for singly-linked list. From 84750aca45f96f15e4dc271410c32cde78667e41 Mon Sep 17 00:00:00 2001 From: 243wresfdxvc Date: Sun, 24 Apr 2022 18:32:09 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200860.=E6=9F=A0?= =?UTF-8?q?=E6=AA=AC=E6=B0=B4=E6=89=BE=E9=9B=B6.md=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0860.柠檬水找零.md | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index ffd5490d..2738f574 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -250,6 +250,49 @@ var lemonadeChange = function(bills) { return true }; +``` +### C +```c +bool lemonadeChange(int* bills, int billsSize){ + // 分别记录五元、十元的数量(二十元不用记录,因为不会用到20元找零) + int fiveCount = 0; int tenCount = 0; + + int i; + for(i = 0; i < billsSize; ++i) { + // 分情况讨论每位顾客的付款 + switch(bills[i]) { + // 情况一:直接收款五元 + case 5: + fiveCount++; + break; + // 情况二:收款十元 + case 10: + // 若没有五元找零,返回false + if(fiveCount == 0) + return false; + // 收款十元并找零五元 + fiveCount--; + tenCount++; + break; + // 情况三:收款二十元 + case 20: + // 若可以,优先用十元和五元找零(因为十元只能找零20,所以需要尽量用掉。而5元能找零十元和二十元) + if(fiveCount > 0 && tenCount > 0) { + fiveCount--; + tenCount--; + } + // 若没有十元,但是有三张五元。用三张五元找零 + else if(fiveCount >= 3) + fiveCount-=3; + // 无法找开,返回false + else + return false; + break; + } + } + // 全部可以找开,返回true + return true; +} ``` ----------------------- From 3ec08f41dd08cdd830215bdf2b602bbdcfc92512 Mon Sep 17 00:00:00 2001 From: 243wresfdxvc Date: Mon, 25 Apr 2022 18:18:50 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200406.=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?= =?UTF-8?q?.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/0406.根据身高重建队列.md | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md index b2354d09..9cefa11c 100644 --- a/problems/0406.根据身高重建队列.md +++ b/problems/0406.根据身高重建队列.md @@ -290,6 +290,53 @@ var reconstructQueue = function(people) { }; ``` +### C +```c +int cmp(const void *p1, const void *p2) { + int *pp1 = *(int**)p1; + int *pp2 = *(int**)p2; + // 若身高相同,则按照k从小到大排列 + // 若身高不同,按身高从大到小排列 + return pp1[0] == pp2[0] ? pp1[1] - pp2[1] : pp2[0] - pp1[0]; +} + +// 将start与end中间的元素都后移一位 +// start为将要新插入元素的位置 +void moveBack(int **people, int peopleSize, int start, int end) { + int i; + for(i = end; i > start; i--) { + people[i] = people[i-1]; + } +} + +int** reconstructQueue(int** people, int peopleSize, int* peopleColSize, int* returnSize, int** returnColumnSizes){ + int i; + // 将people按身高从大到小排列(若身高相同,按k从小到大排列) + qsort(people, peopleSize, sizeof(int*), cmp); + + for(i = 0; i < peopleSize; ++i) { + // people[i]要插入的位置 + int position = people[i][1]; + int *temp = people[i]; + // 将position到i中间的元素后移一位 + // 注:因为已经排好序,position不会比i大。(举例:排序后people最后一位元素最小,其可能的k最大值为peopleSize-2,小于此时的i) + moveBack(people, peopleSize, position, i); + // 将temp放置到position处 + people[position] = temp; + + } + + + // 设置返回二维数组的大小以及里面每个一维数组的长度 + *returnSize = peopleSize; + *returnColumnSizes = (int*)malloc(sizeof(int) * peopleSize); + for(i = 0; i < peopleSize; ++i) { + (*returnColumnSizes)[i] = 2; + } + return people; +} +``` + -----------------------
From 0e5e657a6d5ba057e8dfa77019b25c5404fe9a9b Mon Sep 17 00:00:00 2001 From: 243wresfdxvc Date: Tue, 10 May 2022 23:04:35 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200404.=E5=B7=A6?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C.md=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0404.左叶子之和.md | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 6420da81..d7fd629e 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -466,6 +466,55 @@ func sumOfLeftLeaves(_ root: TreeNode?) -> Int { } ``` +## C +递归法: +```c +int sumOfLeftLeaves(struct TreeNode* root){ + // 递归结束条件:若当前结点为空,返回0 + if(!root) + return 0; + + // 递归取左子树的左结点和和右子树的左结点和 + int leftValue = sumOfLeftLeaves(root->left); + int rightValue = sumOfLeftLeaves(root->right); + + // 若当前结点的左结点存在,且其为叶子结点。取它的值 + int midValue = 0; + if(root->left && (!root->left->left && !root->left->right)) + midValue = root->left->val; + + return leftValue + rightValue + midValue; +} +``` + +迭代法: +```c +int sumOfLeftLeaves(struct TreeNode* root){ + struct TreeNode* stack[1000]; + int stackTop = 0; + + // 若传入root结点不为空,将其入栈 + if(root) + stack[stackTop++] = root; + + int sum = 0; + //若栈不为空,进行循环 + while(stackTop) { + // 出栈栈顶元素 + struct TreeNode *topNode = stack[--stackTop]; + // 若栈顶元素的左孩子为左叶子结点,将其值加入sum中 + if(topNode->left && (!topNode->left->left && !topNode->left->right)) + sum += topNode->left->val; + + // 若当前栈顶结点有左右孩子。将他们加入栈中进行遍历 + if(topNode->right) + stack[stackTop++] = topNode->right; + if(topNode->left) + stack[stackTop++] = topNode->left; + } + return sum; +} +``` -----------------------