用时2分52秒:
用差值进行计算:
public int maxProfit(int[] prices) { int n = prices.length; if(n <= 1) return 0; int interest = 0; for(int i = 1;i<n;i++){ int pri = prices[i]-prices[i-1]; if(pri > 0){ interest += pri; } } return interest; }
——2020.8.5