Skip to content

Commit 045f3f9

Browse files
committed
0134.加油站.md Javascript
1 parent 7b3f8ea commit 045f3f9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

problems/0134.加油站.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,28 @@ class Solution:
241241

242242
Go:
243243

244+
Javascript:
245+
```Javascript
246+
var canCompleteCircuit = function(gas, cost) {
247+
const gasLen = gas.length
248+
let start = 0
249+
let curSum = 0
250+
let totalSum = 0
251+
252+
for(let i = 0; i < gasLen; i++) {
253+
curSum += gas[i] - cost[i]
254+
totalSum += gas[i] - cost[i]
255+
if(curSum < 0) {
256+
curSum = 0
257+
start = i + 1
258+
}
259+
}
260+
261+
if(totalSum < 0) return -1
244262

263+
return start
264+
};
265+
```
245266

246267

247268
-----------------------

0 commit comments

Comments
 (0)