Skip to content

Commit 95a8752

Browse files
committed
更新了121的一维数组优化空间的java代码注释
1 parent 3088601 commit 95a8752

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public:
194194

195195
## 其他语言版本
196196

197-
Java
197+
### Java
198198

199199
> 贪心法:
200200

@@ -242,11 +242,12 @@ class Solution {
242242
class Solution {
243243
public int maxProfit(int[] prices) {
244244
int[] dp = new int[2];
245+
// 记录一次交易,一次交易有买入卖出两种状态
246+
// 0代表持有,1代表卖出
245247
dp[0] = -prices[0];
246248
dp[1] = 0;
247249
// 可以参考斐波那契问题的优化方式
248-
// dp[0] 和 dp[1], 其实是第 0 天的数据
249-
// 所以我们从 i=1 开始遍历数组,一共有 prices.length 天,
250+
// 我们从 i=1 开始遍历数组,一共有 prices.length 天,
250251
// 所以是 i<=prices.length
251252
for (int i = 1; i <= prices.length; i++) {
252253
// 前一天持有;或当天买入
@@ -263,7 +264,7 @@ class Solution {
263264
}
264265
```
265266
266-
Python
267+
### Python
267268
268269
> 贪心法:
269270
```python
@@ -307,7 +308,8 @@ class Solution:
307308
return dp[(length-1) % 2][1]
308309
```
309310

310-
Go:
311+
### Go
312+
311313
```Go
312314
func maxProfit(prices []int) int {
313315
length:=len(prices)
@@ -334,7 +336,7 @@ func max(a,b int)int {
334336
}
335337
```
336338

337-
JavaScript
339+
### JavaScript
338340

339341
> 动态规划
340342

0 commit comments

Comments
 (0)