From cf0affde0826eedf6328cf383d89d70028bf43a6 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 19:30:51 +0800
Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200509.=E6=96=90?=
=?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0509.斐波那契数.md | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md
index 1d17784d..d8e4e1d7 100644
--- a/problems/0509.斐波那契数.md
+++ b/problems/0509.斐波那契数.md
@@ -245,7 +245,7 @@ var fib = function(n) {
};
```
-TypeScript
+### TypeScript
```typescript
function fib(n: number): number {
@@ -324,5 +324,33 @@ pub fn fib(n: i32) -> i32 {
return fib(n - 1) + fib(n - 2);
}
```
+
+### Scala
+
+动态规划:
+```scala
+object Solution {
+ def fib(n: Int): Int = {
+ if (n <= 1) return n
+ var dp = new Array[Int](n + 1)
+ dp(1) = 1
+ for (i <- 2 to n) {
+ dp(i) = dp(i - 1) + dp(i - 2)
+ }
+ dp(n)
+ }
+}
+```
+
+递归:
+```scala
+object Solution {
+ def fib(n: Int): Int = {
+ if (n <= 1) return n
+ fib(n - 1) + fib(n - 2)
+ }
+}
+```
+
-----------------------
From 5985aae83ce72f2d25b4976a836c1a5fd39ac1ef Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 19:37:35 +0800
Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200070.=E7=88=AC?=
=?UTF-8?q?=E6=A5=BC=E6=A2=AF.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0070.爬楼梯.md | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md
index 34d41441..097466b0 100644
--- a/problems/0070.爬楼梯.md
+++ b/problems/0070.爬楼梯.md
@@ -401,6 +401,38 @@ int climbStairs(int n){
}
```
+### Scala
+
+```scala
+object Solution {
+ def climbStairs(n: Int): Int = {
+ if (n <= 2) return n
+ var dp = new Array[Int](n + 1)
+ dp(1) = 1
+ dp(2) = 2
+ for (i <- 3 to n) {
+ dp(i) = dp(i - 1) + dp(i - 2)
+ }
+ dp(n)
+ }
+}
+```
+
+优化空间复杂度:
+```scala
+object Solution {
+ def climbStairs(n: Int): Int = {
+ if (n <= 2) return n
+ var (a, b) = (1, 2)
+ for (i <- 3 to n) {
+ var tmp = a + b
+ a = b
+ b = tmp
+ }
+ b // 最终返回b
+ }
+}
+```
-----------------------
From 6e92cd2417dea438d7671140fd73298a4b598c93 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 20:12:19 +0800
Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200746.=E4=BD=BF?=
=?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?=
=?UTF-8?q?=E6=A2=AF.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0746.使用最小花费爬楼梯.md | 30 ++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md
index 5931fc8a..abaeb980 100644
--- a/problems/0746.使用最小花费爬楼梯.md
+++ b/problems/0746.使用最小花费爬楼梯.md
@@ -305,5 +305,35 @@ int minCostClimbingStairs(int* cost, int costSize){
return dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2];
}
```
+
+### Scala
+
+```scala
+object Solution {
+ def minCostClimbingStairs(cost: Array[Int]): Int = {
+ var dp = new Array[Int](cost.length)
+ dp(0) = cost(0)
+ dp(1) = cost(1)
+ for (i <- 2 until cost.length) {
+ dp(i) = math.min(dp(i - 1), dp(i - 2)) + cost(i)
+ }
+ math.min(dp(cost.length - 1), dp(cost.length - 2))
+ }
+}
+```
+
+第二种思路: dp[i] 表示爬到第i-1层所需的最小花费,状态转移方程为: dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2])
+```scala
+object Solution {
+ def minCostClimbingStairs(cost: Array[Int]): Int = {
+ var dp = new Array[Int](cost.length + 1)
+ for (i <- 2 until cost.length + 1) {
+ dp(i) = math.min(dp(i - 1) + cost(i - 1), dp(i - 2) + cost(i - 2))
+ }
+ dp(cost.length)
+ }
+}
+```
+
-----------------------