From 95a8752dd5d3c522b16b237a37df09e03379b35e Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 16:19:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86121=E7=9A=84?= =?UTF-8?q?=E4=B8=80=E7=BB=B4=E6=95=B0=E7=BB=84=E4=BC=98=E5=8C=96=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E7=9A=84java=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0121.买卖股票的最佳时机.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 45b61666..2815cd47 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -194,7 +194,7 @@ public: ## 其他语言版本 -Java: +### Java > 贪心法: @@ -242,11 +242,12 @@ class Solution { class Solution { public int maxProfit(int[] prices) { int[] dp = new int[2]; + // 记录一次交易,一次交易有买入卖出两种状态 + // 0代表持有,1代表卖出 dp[0] = -prices[0]; dp[1] = 0; // 可以参考斐波那契问题的优化方式 - // dp[0] 和 dp[1], 其实是第 0 天的数据 - // 所以我们从 i=1 开始遍历数组,一共有 prices.length 天, + // 我们从 i=1 开始遍历数组,一共有 prices.length 天, // 所以是 i<=prices.length for (int i = 1; i <= prices.length; i++) { // 前一天持有;或当天买入 @@ -263,7 +264,7 @@ class Solution { } ``` -Python: +### Python > 贪心法: ```python @@ -307,7 +308,8 @@ class Solution: return dp[(length-1) % 2][1] ``` -Go: +### Go + ```Go func maxProfit(prices []int) int { length:=len(prices) @@ -334,7 +336,7 @@ func max(a,b int)int { } ``` -JavaScript: +### JavaScript > 动态规划