Merge pull request #685 from KingArthur0205/remote

添加 0134.加油站.md C语言版本、添加 0134.加油站.md C语言版本注释、以及 添加 0452.用最少数量的箭引爆气球.md C语言版本
This commit is contained in:
程序员Carl
2021-08-31 10:52:09 +08:00
committed by GitHub
2 changed files with 53 additions and 0 deletions

View File

@ -283,6 +283,35 @@ var canCompleteCircuit = function(gas, cost) {
};
```
C:
```c
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
int curSum = 0;
int i;
int min = INT_MAX;
//遍历整个数组。计算出每站的用油差。并将其与最小累加量比较
for(i = 0; i < gasSize; i++) {
int diff = gas[i] - cost[i];
curSum += diff;
if(curSum < min)
min = curSum;
}
//若汽油总数为负数,代表无法跑完一环。返回-1
if(curSum < 0)
return -1;
//若min大于等于0说明每一天加油量比用油量多。因此从0出发即可
if(min >= 0)
return 0;
//若累加最小值为负,则找到一个非零元素(加油量大于出油量)出发。返回坐标
for(i = gasSize - 1; i >= 0; i--) {
min+=(gas[i]-cost[i]);
if(min >= 0)
return i;
}
//逻辑上不会返回这个0
return 0;
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

View File

@ -218,6 +218,30 @@ var findMinArrowShots = function(points) {
};
```
C:
```c
int cmp(const void *a,const void *b)
{
return ((*((int**)a))[0] > (*((int**)b))[0]);
}
int findMinArrowShots(int** points, int pointsSize, int* pointsColSize){
//将points数组作升序排序
qsort(points, pointsSize, sizeof(points[0]),cmp);
int arrowNum = 1;
int i = 1;
for(i = 1; i < pointsSize; i++) {
//若前一个气球与当前气球不重叠,证明需要增加箭的数量
if(points[i][0] > points[i-1][1])
arrowNum++;
else
//若前一个气球与当前气球重叠判断并更新最小的x_end
points[i][1] = points[i][1] > points[i-1][1] ? points[i-1][1] : points[i][1];
}
return arrowNum;
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)