Best Time to Buy and Sell Stock II 买卖股票最好的时间II

来源:互联网 发布:数据库域 编辑:程序博客网 时间:2024/04/25 11:27
class Solution {
public:
//贪心法,低进高出,把所有正的价格差价相加起来。
    int maxProfit(vector<int> &prices) {
    int n=prices.size();
    if(n<2)
    return 0;
    int profit=0;
    int minbuy=prices[0];
    for(int i=1;i<prices.size();i++)
    {
      if(prices[i]>prices[i-1])
        profit+=prices[i]-prices[i-1];
    }
    return profit;
    }
};
0 0
原创粉丝点击