Merge pull request #1245 from KingArthur0205/remote

添加 0134. 加油站.md, 0203.移除链表元素.md, 0860.柠檬水找零.md, 0406.根据身高重建队列.md, 0404.左叶子之和.md C语言解法
This commit is contained in:
程序员Carl
2022-05-12 09:21:49 +08:00
committed by GitHub
5 changed files with 207 additions and 0 deletions

View File

@ -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;
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -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.

View File

@ -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;
}
```
-----------------------

View File

@ -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[][] {
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -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