From 381c4e1bdf8c2b894e88817a79ffd388144c164c Mon Sep 17 00:00:00 2001 From: yangxk201396 <54167260+yangxk201396@users.noreply.github.com> Date: Wed, 16 Jun 2021 10:43:50 +0800 Subject: [PATCH] =?UTF-8?q?Update=200134.=E5=8A=A0=E6=B2=B9=E7=AB=99.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0134.加油站.md Golang 版本 --- problems/0134.加油站.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index dfed2d96..9b660ea0 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -240,6 +240,25 @@ class Solution: ``` Go: +```go +func canCompleteCircuit(gas []int, cost []int) int { + curSum := 0 + totalSum := 0 + start := 0 + for i := 0; i < len(gas); i++ { + curSum += gas[i] - cost[i] + totalSum += gas[i] - cost[i] + if curSum < 0 { + start = i+1 + curSum = 0 + } + } + if totalSum < 0 { + return -1 + } + return start +} +``` Javascript: ```Javascript