LeetCode:122 买卖股票的最佳时机 Ⅱ

class Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        int res = 0;
        int i = 0;
        while(i < len - 1){
            while(i < len - 1 && prices[i] >= prices[i+1]){
                i++;
                
            }
            int t = prices[i];
            while(i < len - 1 && prices[i] <= prices[i+1]){
                i++;
            }
            int x = prices[i];
            res += x - t;

       
        }
        return res;
    }
}

 

posted @ 2020-10-17 15:23  dlooooo  阅读(69)  评论(0编辑  收藏  举报