File tree Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Original file line number Diff line number Diff line change @@ -194,7 +194,7 @@ public:
194
194
195
195
## 其他语言版本
196
196
197
- Java:
197
+ ### Java
198
198
199
199
> 贪心法:
200
200
@@ -242,11 +242,12 @@ class Solution {
242
242
class Solution {
243
243
public int maxProfit(int[ ] prices) {
244
244
int[ ] dp = new int[ 2] ;
245
+ // 记录一次交易,一次交易有买入卖出两种状态
246
+ // 0代表持有,1代表卖出
245
247
dp[ 0] = -prices[ 0] ;
246
248
dp[ 1] = 0;
247
249
// 可以参考斐波那契问题的优化方式
248
- // dp[ 0] 和 dp[ 1] , 其实是第 0 天的数据
249
- // 所以我们从 i=1 开始遍历数组,一共有 prices.length 天,
250
+ // 我们从 i=1 开始遍历数组,一共有 prices.length 天,
250
251
// 所以是 i<=prices.length
251
252
for (int i = 1; i <= prices.length; i++) {
252
253
// 前一天持有;或当天买入
@@ -263,7 +264,7 @@ class Solution {
263
264
}
264
265
```
265
266
266
- Python:
267
+ ### Python
267
268
268
269
> 贪心法:
269
270
```python
@@ -307,7 +308,8 @@ class Solution:
307
308
return dp[(length-1) % 2][1]
308
309
```
309
310
310
- Go:
311
+ ### Go
312
+
311
313
``` Go
312
314
func maxProfit (prices []int ) int {
313
315
length := len (prices)
@@ -334,7 +336,7 @@ func max(a,b int)int {
334
336
}
335
337
```
336
338
337
- JavaScript:
339
+ ### JavaScript
338
340
339
341
> 动态规划
340
342
You can’t perform that action at this time.
0 commit comments