diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index e96773ff..a88f677d 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -408,6 +408,9 @@ function canCompleteCircuit(gas: number[], cost: number[]): number { ### C +贪心算法:方法一 + + ```c int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ int curSum = 0; @@ -437,5 +440,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; +} +``` + -----------------------
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. 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; +} +``` ----------------------- diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md index ecb05301..641086a9 100644 --- a/problems/0406.根据身高重建队列.md +++ b/problems/0406.根据身高重建队列.md @@ -290,6 +290,54 @@ 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; +} +``` + ### TypeScript ```typescript @@ -309,5 +357,6 @@ function reconstructQueue(people: number[][]): number[][] { + -----------------------
diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index 5d5d6ad2..aa09e1c6 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; +} ``` ### TypeScript