mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
0134.加油站.md Javascript
This commit is contained in:
@ -241,7 +241,28 @@ class Solution:
|
||||
|
||||
Go:
|
||||
|
||||
Javascript:
|
||||
```Javascript
|
||||
var canCompleteCircuit = function(gas, cost) {
|
||||
const gasLen = gas.length
|
||||
let start = 0
|
||||
let curSum = 0
|
||||
let totalSum = 0
|
||||
|
||||
for(let i = 0; i < gasLen; i++) {
|
||||
curSum += gas[i] - cost[i]
|
||||
totalSum += gas[i] - cost[i]
|
||||
if(curSum < 0) {
|
||||
curSum = 0
|
||||
start = i + 1
|
||||
}
|
||||
}
|
||||
|
||||
if(totalSum < 0) return -1
|
||||
|
||||
return start
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user