diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index ff1b6d4a..29aa5b83 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -139,17 +139,11 @@ Java: // 贪心思路 class Solution { public int maxProfit(int[] prices) { - int sum = 0; - int profit = 0; - int buy = prices[0]; + int result = 0; for (int i = 1; i < prices.length; i++) { - profit = prices[i] - buy; - if (profit > 0) { - sum += profit; - } - buy = prices[i]; + result += Math.max(prices[i] - prices[i - 1], 0); } - return sum; + return result; } } ```