Advertisement

买卖股票的最佳时机(C++题解含VS可运行源码)

阅读量:

121. 买卖股票的最佳时机(C++题解含VS可运行源码)

  • 1.力扣平台C++编程代码
    • 2.题目解析内容
    • 3.可在Visual Studio环境中直接运行的源代码

1.力扣C++源码

复制代码
    class Solution {
    public:
    int maxProfit(vector<int>& prices) {
        int maxprofit = 0;//最大利润设为0
        int minprice = INT_MAX;//最低价格设为一个很大的数
        for (int i = 0; i < prices.size(); i++) {
            maxprofit = max(maxprofit, prices[i] - minprice);
            minprice = min(minprice, prices[i]);
        }
        return maxprofit;
    }
    };
    
    
      
      
      
      
      
      
      
      
      
      
      
      
    

2.

全部评论 (0)

还没有任何评论哟~