添加 0134.加油站.md C语言版本

This commit is contained in:
ArthurP
2021-08-28 22:56:15 +08:00
parent 61c3670473
commit 417d5b80bf

View File

@ -283,6 +283,30 @@ 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(min > curSum)
min = curSum;
}
if(curSum < 0)
return -1;
if(min >= 0)
return 0;
for(i = gasSize - 1; i >= 0; i--) {
min+=(gas[i]-cost[i]);
if(min >= 0)
return i;
}
return 0;
}
```
----------------------- -----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)