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) { diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index a18c008d..0d5a7075 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -183,6 +183,45 @@ class Solution: Go: +```golang +func lemonadeChange(bills []int) bool { + //left表示还剩多少 下表0位5元的个数 ,下表1为10元的个数 + left:=[2]int{0,0} + //第一个元素不为5,直接退出 + if bills[0]!=5{ + return false + } + for i:=0;i0{ + left[0]-=1 + }else { + return false + } + } + if tmp==15{ + if left[1]>0&&left[0]>0{ + left[0]-=1 + left[1]-=1 + }else if left[1]==0&&left[0]>2{ + left[0]-=3 + }else{ + return false + } + } + } + return true +} +``` Javascript: ```Javascript