From 7221797cca0a206f469732c3dc5a06c3ef9d3885 Mon Sep 17 00:00:00 2001 From: yangxk201396 <54167260+yangxk201396@users.noreply.github.com> Date: Wed, 16 Jun 2021 02:06:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=201005.K=E6=AC=A1=E5=8F=96=E5=8F=8D?= =?UTF-8?q?=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 1005.K次取反后最大化的数组和.md Golang版本 --- ...1005.K次取反后最大化的数组和.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 387de147..c3e99f7e 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -138,6 +138,30 @@ class Solution: ``` Go: +```Go +func largestSumAfterKNegations(nums []int, K int) int { + sort.Slice(nums, func(i, j int) bool { + return math.Abs(float64(nums[i])) > math.Abs(float64(nums[j])) + }) + + for i := 0; i < len(nums); i++ { + if K > 0 && nums[i] < 0 { + nums[i] = -nums[i] + K-- + } + } + + if K%2 == 1 { + nums[len(nums)-1] = -nums[len(nums)-1] + } + + result := 0 + for i := 0; i < len(nums); i++ { + result += nums[i] + } + return result +} +``` Javascript: 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 2/2] =?UTF-8?q?Update=200134.=E5=8A=A0=E6=B2=B9=E7=AB=99.m?= =?UTF-8?q?d?= 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