码迷,mamicode.com
首页 > 其他好文 > 详细

121. 买卖股票的最佳时机

时间:2020-04-01 19:41:00      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:max   cto   turn   price   pre   bsp   股票   solution   int   

 1 class Solution 
 2 {
 3 public:
 4     int maxProfit(vector<int>& prices) 
 5     {
 6         if(prices.empty()) return 0;
 7         int n = prices.size();
 8         int buy = prices[0];
 9         int res = 0;
10         for(int i = 1;i < n;i ++)
11         {
12             res = max(res,prices[i] - buy);
13             if(buy > prices[i]) buy = prices[i];
14         }
15         return res;
16     }
17 };

 

121. 买卖股票的最佳时机

标签:max   cto   turn   price   pre   bsp   股票   solution   int   

原文地址:https://www.cnblogs.com/yuhong1103/p/12614540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!