LeetCode_122. 买卖股票的最佳时机 II

利润的潜台词就是前一天比后一天小就进行买入和卖出的操作。
public class S_122 {
    public int maxProfit(int[] prices) {
        // 先圈定特殊情况
        if (prices == null || prices.length < 1) {
            return 0;
        }
        // 定义一个量记录利润
        int count = 0;
        // 遍历 进行前后数的相减 大于零即有利润就存入count
        for(int i = 1;i < prices.length;i++){
            if(prices[i] - prices[i-1] > 0){
                count += prices[i] - prices[i-1];
            }
        }
        // 返回count的值
        return count;
    }
}

猜你喜欢

转载自blog.csdn.net/king1994wzl/article/details/82810517