From ed557367383ab0df57e47074fcd751bcaba2308d Mon Sep 17 00:00:00 2001 From: reoaah Date: Thu, 20 May 2021 14:37:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A00509.=E6=96=90=E6=B3=A2?= =?UTF-8?q?=E9=82=A3=E5=A5=91=E6=95=B0.md=20Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0509.斐波那契数.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 112c96f9..5afcd6b8 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -207,6 +207,19 @@ class Solution: ``` Go: +```Go +func fib(n int) int { + if n < 2 { + return n + } + a, b, c := 0, 1, 0 + for i := 1; i < n; i++ { + c = a + b + a, b = b, c + } + return c +} +``` From e67b7acaecb03440cf644930a69f9f45bff04743 Mon Sep 17 00:00:00 2001 From: reoaah Date: Thu, 20 May 2021 15:25:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A00746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md=20Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index b8158205..aff56208 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -228,7 +228,23 @@ Python: Go: +```Go +func minCostClimbingStairs(cost []int) int { + dp := make([]int, len(cost)) + dp[0], dp[1] = cost[0], cost[1] + for i := 2; i < len(cost); i++ { + dp[i] = min(dp[i-1], dp[i-2]) + cost[i] + } + return min(dp[len(cost)-1], dp[len(cost)-2]) +} +func min(a, b int) int { + if a < b { + return a + } + return b +} +```