数组后一个减去前一个,为正数就加起来
public class Solution { public int maxProfit (int[] prices) { int sum = 0; for (int i = 1; i < prices.length; i++) { int profit = prices[i] - prices[i-1]; if (profit > 0) { sum += profit; } } return sum; } }
数组后一个减去前一个,为正数就加起来
public class Solution { public int maxProfit (int[] prices) { int sum = 0; for (int i = 1; i < prices.length; i++) { int profit = prices[i] - prices[i-1]; if (profit > 0) { sum += profit; } } return sum; } }