best-time-to-buy-and-sell-stock-ii

//给定数组,统计各个数字的最大和。

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int res = 0;
        int n = prices.size();
        for (int i = 0; i < n - 1; ++i) {
            if (prices[i] < prices[i + 1]) {
                res += prices[i + 1] - prices[i];
            }
        }
        return res;
    }
};

 

posted on 2017-03-07 01:40  123_123  阅读(93)  评论(0编辑  收藏  举报