From aa5498df7882df5cd42eedb34f96df9c662b0eb2 Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 16:22:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BA=86122?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E7=BB=B4=E6=95=B0=E7=BB=84java=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E5=85=B6=E5=AE=9E=E5=B0=B1=E6=98=AF=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E7=A9=BA=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0122.买卖股票的最佳时机II.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 5b117563..e2464a03 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -171,15 +171,15 @@ class Solution { // 动态规划 // 优化空间 class Solution { public int maxProfit(int[] prices) { - int[] dp=new int[2]; + int[] dp = new int[2]; // 0表示持有,1表示卖出 - dp[0]=-prices[0]; - dp[1]=0; - for(int i=1; i<=prices.length; i++){ + dp[0] = -prices[0]; + dp[1] = 0; + for(int i = 1; i <= prices.length; i++){ // 前一天持有; 或当天卖出然后买入 - dp[0]=Math.max(dp[0], dp[1]-prices[i-1]); + dp[0] = Math.max(dp[0], dp[1] - prices[i-1]); // 前一天卖出; 或当天卖出,当天卖出,得先持有 - dp[1]=Math.max(dp[1], dp[0]+prices[i-1]); + dp[1] = Math.max(dp[1], dp[0] + prices[i-1]); } return dp[1]; }