From bd4f69d04b663dae39ab5a384722cd7d5aae3377 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 11 Jun 2022 16:35:06 +0800
Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C=EF=BC=88=E5=8A=A8=E6=80=81?=
=?UTF-8?q?=E8=A7=84=E5=88=92=EF=BC=89.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0053.最大子序和(动态规划).md | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/problems/0053.最大子序和(动态规划).md b/problems/0053.最大子序和(动态规划).md
index 4c883cb6..6712d0ac 100644
--- a/problems/0053.最大子序和(动态规划).md
+++ b/problems/0053.最大子序和(动态规划).md
@@ -186,7 +186,22 @@ const maxSubArray = nums => {
};
```
+Scala:
+```scala
+object Solution {
+ def maxSubArray(nums: Array[Int]): Int = {
+ var dp = new Array[Int](nums.length)
+ var result = nums(0)
+ dp(0) = nums(0)
+ for (i <- 1 until nums.length) {
+ dp(i) = math.max(nums(i), dp(i - 1) + nums(i))
+ result = math.max(result, dp(i)) // 更新最大值
+ }
+ result
+ }
+}
+```
-----------------------
From 9031523bbb334cb2190b17f12333d3a5f1edda53 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 11 Jun 2022 16:40:33 +0800
Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C.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/0053.最大子序和.md | 33 ++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md
index 73cac244..4971133f 100644
--- a/problems/0053.最大子序和.md
+++ b/problems/0053.最大子序和.md
@@ -333,8 +333,41 @@ function maxSubArray(nums: number[]): number {
};
```
+### Scala
+**贪心**
+```scala
+object Solution {
+ def maxSubArray(nums: Array[Int]): Int = {
+ var result = Int.MinValue
+ var count = 0
+ for (i <- nums.indices) {
+ count += nums(i) // count累加
+ if (count > result) result = count // 记录最大值
+ if (count <= 0) count = 0 // 一旦count为负,则count归0
+ }
+ result
+ }
+}
+```
+
+**动态规划**
+
+```scala
+object Solution {
+ def maxSubArray(nums: Array[Int]): Int = {
+ var dp = new Array[Int](nums.length)
+ var result = nums(0)
+ dp(0) = nums(0)
+ for (i <- 1 until nums.length) {
+ dp(i) = math.max(nums(i), dp(i - 1) + nums(i))
+ result = math.max(result, dp(i)) // 更新最大值
+ }
+ result
+ }
+}
+```
-----------------------
From ff233a8e3ccfb08e82ebaa662c93bf687b76f153 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 11 Jun 2022 16:53:35 +0800
Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=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.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0122.买卖股票的最佳时机II.md | 35 ++++++++++++++-----
1 file changed, 27 insertions(+), 8 deletions(-)
diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md
index 1e7b77d8..1369ff5b 100644
--- a/problems/0122.买卖股票的最佳时机II.md
+++ b/problems/0122.买卖股票的最佳时机II.md
@@ -133,8 +133,9 @@ public:
## 其他语言版本
-Java:
+### Java:
+贪心:
```java
// 贪心思路
class Solution {
@@ -148,6 +149,7 @@ class Solution {
}
```
+动态规划:
```java
class Solution { // 动态规划
public int maxProfit(int[] prices) {
@@ -169,8 +171,8 @@ class Solution { // 动态规划
}
```
-Python:
-
+### Python:
+贪心:
```python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
@@ -180,7 +182,7 @@ class Solution:
return result
```
-python动态规划
+动态规划:
```python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
@@ -194,7 +196,7 @@ class Solution:
return dp[-1][1]
```
-Go:
+### Go:
```golang
//贪心算法
@@ -231,7 +233,7 @@ func maxProfit(prices []int) int {
}
```
-Javascript:
+### Javascript:
贪心
```Javascript
@@ -268,7 +270,7 @@ const maxProfit = (prices) => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function maxProfit(prices: number[]): number {
@@ -280,7 +282,7 @@ function maxProfit(prices: number[]): number {
};
```
-C:
+### C:
贪心:
```c
int maxProfit(int* prices, int pricesSize){
@@ -318,5 +320,22 @@ int maxProfit(int* prices, int pricesSize){
}
```
+### Scala
+
+贪心:
+```scala
+object Solution {
+ def maxProfit(prices: Array[Int]): Int = {
+ var result = 0
+ for (i <- 1 until prices.length) {
+ if (prices(i) > prices(i - 1)) {
+ result += prices(i) - prices(i - 1)
+ }
+ }
+ result
+ }
+}
+```
+
-----------------------