Merge pull request #313 from fusunx/master

0134.加油站.md Javascript
This commit is contained in:
Carl Sun 2021-06-02 20:45:32 +08:00 committed by GitHub
commit ff2ec86166
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 0 deletions

View File

@ -241,7 +241,28 @@ class Solution:
Go 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
};
```
----------------------- -----------------------