十一第二题

来源:互联网 发布:ipad办公 知乎 编辑:程序博客网 时间:2024/06/13 03:13

public class Solution {    public int maxProfit(int[] prices) {        if (prices == null || prices.length == 0)            return 0;        int sum = 0;        for (int i = 0; i < prices.length - 1; i ++) {            if (prices[i] < prices[i + 1])                sum += prices[i + 1] - prices[i];        }                return sum;    }}

只要获取所有的峰谷和峰值就可以了。


0 0