From 9bd3c4d3549516cd469e9b4ab8580c326741479a Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Wed, 21 Jul 2021 22:32:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A00455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加0455.分发饼干 go版本 --- problems/0455.分发饼干.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 4814d414..6b121e36 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -146,6 +146,23 @@ class Solution: return res ``` Go: +```golang +//排序后,局部最优 +func findContentChildren(g []int, s []int) int { + sort.Ints(g) + sort.Ints(s) + + // 从小到大 + child := 0 + for sIdx := 0; child < len(g) && sIdx < len(s); sIdx++ { + if s[sIdx] >= g[child] {//如果饼干的大小大于或等于孩子的为空则给与,否则不给予,继续寻找选一个饼干是否符合 + child++ + } + } + + return child +} + Javascript: ```Javascript From 1176b756a9b82c282c5aa05c539402f6805d9f85 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Fri, 23 Jul 2021 10:55:16 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 0376.摆动序列 go版本 --- problems/0376.摆动序列.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 4d283eb0..f64e0043 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -151,7 +151,24 @@ class Solution: ``` Go: - +```golang +func wiggleMaxLength(nums []int) int { + var count,preDiff,curDiff int + count=1 + if len(nums)<2{ + return count + } + for i:=0;i 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0){ + preDiff=curDiff + count++ + } + } + return count +} +``` Javascript: ```Javascript From 65363329f02cefb67909e781c326bffc7fe5b1eb Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Fri, 23 Jul 2021 14:00:03 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200122.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BAII=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 0122.买卖股票的最佳时机II go版本 --- .../0122.买卖股票的最佳时机II.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 4b878aa0..60d4591f 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -188,7 +188,40 @@ class Solution: ``` Go: +```golang +//贪心算法 +func maxProfit(prices []int) int { + var sum int + for i := 1; i < len(prices); i++ { + // 累加每次大于0的交易 + if prices[i]-prices[i-1] > 0 { + sum += prices[i]-prices[i-1] + } + } + return sum +} +``` +```golang +//确定售卖点 +func maxProfit(prices []int) int { + var result,buy int + prices=append(prices,0)//在price末尾加个0,防止price一直递增 + /** + 思路:检查后一个元素是否大于当前元素,如果小于,则表明这是一个售卖点,当前元素的值减去购买时候的值 + 如果不小于,说明后面有更好的售卖点, + **/ + for i:=0;iprices[i+1]{ + result+=prices[i]-prices[buy] + buy=i+1 + }else if prices[buy]>prices[i]{//更改最低购买点 + buy=i + } + } + return result +} +``` Javascript: ```Javascript