Skip to content

Commit ff233a8

Browse files
committed
添加 0122.买卖股票的最佳时机II.md Scala版本
1 parent 9031523 commit ff233a8

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

problems/0122.买卖股票的最佳时机II.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ public:
133133

134134
## 其他语言版本
135135

136-
Java:
136+
### Java:
137137

138+
贪心:
138139
```java
139140
// 贪心思路
140141
class Solution {
@@ -148,6 +149,7 @@ class Solution {
148149
}
149150
```
150151

152+
动态规划:
151153
```java
152154
class Solution { // 动态规划
153155
public int maxProfit(int[] prices) {
@@ -169,8 +171,8 @@ class Solution { // 动态规划
169171
}
170172
```
171173

172-
Python:
173-
174+
### Python:
175+
贪心:
174176
```python
175177
class Solution:
176178
def maxProfit(self, prices: List[int]) -> int:
@@ -180,7 +182,7 @@ class Solution:
180182
return result
181183
```
182184

183-
python动态规划
185+
动态规划:
184186
```python
185187
class Solution:
186188
def maxProfit(self, prices: List[int]) -> int:
@@ -194,7 +196,7 @@ class Solution:
194196
return dp[-1][1]
195197
```
196198

197-
Go:
199+
### Go:
198200

199201
```golang
200202
//贪心算法
@@ -231,7 +233,7 @@ func maxProfit(prices []int) int {
231233
}
232234
```
233235

234-
Javascript:
236+
### Javascript:
235237

236238
贪心
237239
```Javascript
@@ -268,7 +270,7 @@ const maxProfit = (prices) => {
268270
};
269271
```
270272

271-
TypeScript:
273+
### TypeScript:
272274

273275
```typescript
274276
function maxProfit(prices: number[]): number {
@@ -280,7 +282,7 @@ function maxProfit(prices: number[]): number {
280282
};
281283
```
282284

283-
C:
285+
### C:
284286
贪心:
285287
```c
286288
int maxProfit(int* prices, int pricesSize){
@@ -318,5 +320,22 @@ int maxProfit(int* prices, int pricesSize){
318320
}
319321
```
320322

323+
### Scala
324+
325+
贪心:
326+
```scala
327+
object Solution {
328+
def maxProfit(prices: Array[Int]): Int = {
329+
var result = 0
330+
for (i <- 1 until prices.length) {
331+
if (prices(i) > prices(i - 1)) {
332+
result += prices(i) - prices(i - 1)
333+
}
334+
}
335+
result
336+
}
337+
}
338+
```
339+
321340
-----------------------
322341
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)