题目 思路 一次遍历 具体实现 class Solution { public int maxProfit(int[] prices) { int len = prices.length; int cha = 0; int maxProfit = 0; for(int i = 0;i < len-1;i++){ cha = Math.max(0,cha + prices[i+1]-prices[i]); maxProfit = Math.max(maxProfit,cha); } return maxProfit; } } 复杂度 时间复杂度:O(N)空间复杂度:O(1)