mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0134.加油站.md C语言版本
This commit is contained in:
@ -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)
|
||||||
|
Reference in New Issue
Block a user