@@ -133,8 +133,9 @@ public:
133
133
134
134
## 其他语言版本
135
135
136
- Java:
136
+ ### Java:
137
137
138
+ 贪心:
138
139
``` java
139
140
// 贪心思路
140
141
class Solution {
@@ -148,6 +149,7 @@ class Solution {
148
149
}
149
150
```
150
151
152
+ 动态规划:
151
153
``` java
152
154
class Solution { // 动态规划
153
155
public int maxProfit (int [] prices ) {
@@ -169,8 +171,8 @@ class Solution { // 动态规划
169
171
}
170
172
```
171
173
172
- Python:
173
-
174
+ ### Python:
175
+ 贪心:
174
176
``` python
175
177
class Solution :
176
178
def maxProfit (self , prices : List[int ]) -> int :
@@ -180,7 +182,7 @@ class Solution:
180
182
return result
181
183
```
182
184
183
- python动态规划
185
+ 动态规划:
184
186
``` python
185
187
class Solution :
186
188
def maxProfit (self , prices : List[int ]) -> int :
@@ -194,7 +196,7 @@ class Solution:
194
196
return dp[- 1 ][1 ]
195
197
```
196
198
197
- Go:
199
+ ### Go:
198
200
199
201
``` golang
200
202
// 贪心算法
@@ -231,7 +233,7 @@ func maxProfit(prices []int) int {
231
233
}
232
234
```
233
235
234
- Javascript:
236
+ ### Javascript:
235
237
236
238
贪心
237
239
``` Javascript
@@ -268,7 +270,7 @@ const maxProfit = (prices) => {
268
270
};
269
271
```
270
272
271
- TypeScript:
273
+ ### TypeScript:
272
274
273
275
``` typescript
274
276
function maxProfit(prices : number []): number {
@@ -280,7 +282,7 @@ function maxProfit(prices: number[]): number {
280
282
};
281
283
```
282
284
283
- C:
285
+ ### C:
284
286
贪心:
285
287
``` c
286
288
int maxProfit (int* prices, int pricesSize){
@@ -318,5 +320,22 @@ int maxProfit(int* prices, int pricesSize){
318
320
}
319
321
```
320
322
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
+
321
340
-----------------------
322
341
<div align =" center " ><img src =https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width =500 > </img ></div >
0 commit comments