LeetCode算法题–买卖股票的最佳时机 II 题目来源:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目要求 题目思路: 贪心算法可破之:只要有利润,就出售。 class Solution { public int maxProfit(int[] prices) { int totalProfit = 0; for(int i = 1; i < prices.length; ++i){ if(prices[i] > prices[i - 1]){ totalProfit += prices[i] - prices[i - 1]; } } return totalProfit; } } 执行结果: 时间复杂度:O(n)