From 045f3f98d6234e548c8b98edee911f2eb39b482f Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Wed, 2 Jun 2021 20:10:28 +0800 Subject: [PATCH] =?UTF-8?q?0134.=E5=8A=A0=E6=B2=B9=E7=AB=99.md=20Javascrip?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 5c3f70a8..dfed2d96 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -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 +}; +``` -----------------------