From c08686cc6b26fdd92bb4726080edd0a4ee1e434e Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Tue, 27 Jul 2021 10:48:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200135.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E7=B3=96=E6=9E=9C=20GO=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0135.分发糖果 GO版本 --- problems/0135.分发糖果.md | 38 ++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index fd791277..2d3fca84 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -175,7 +175,43 @@ class Solution: ``` Go: - +```golang +func candy(ratings []int) int { + /**先确定一边,再确定另外一边 + 1.先从左到右,当右边的大于左边的就加1 + 2.再从右到左,当左边的大于右边的就再加1 + **/ + need:=make([]int,len(ratings)) + sum:=0 + //初始化(每个人至少一个糖果) + for i:=0;i0;i--{ + if ratings[i-1]>ratings[i]{ + need[i-1]=findMax(need[i-1],need[i]+1) + } + } + //计算总共糖果 + for i:=0;inum2{ + return num1 + } + return num2 +} +``` Javascript: ```Javascript var candy = function(ratings) {