leetcode 122. 买卖股票的最佳时机 II c++

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/glw0223/article/details/88833394

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

分析

  • 这个题真正的意思是:你知道第二天的价格和第一天的价格,如果是相减是挣钱的,就买。这题真是无语了。
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int result = 0;
        if (prices.empty()) return result;
        for (unsigned int i = 0; i < prices.size() - 1; ++i)
        {
            if (prices[i] < prices[i+1]) 
                result += prices[i+1] - prices[i];
        }
        return result;
    }
};

感谢:https://github.com/glw0223/Leetcode-1/blob/master/src/0122-Best-Time-to-Buy-and-Sell-Stock-II/0122.cpp

猜你喜欢

转载自blog.csdn.net/glw0223/article/details/88833394