首页 > 其他 > 详细

Best Time to Buy and Sell Stock系列

时间:2017-10-29 17:13:20      阅读:275      评论:0      收藏:0      [点我收藏+]
// Best Time to Buy and Sell Stock 1
class Solution { public: int maxProfit(vector<int>& prices) { if(prices.size()==0) return 0; int maxres=prices[prices.size()-1]; int ans=0; for(int i=prices.size()-1;i>=0;i--) { maxres=max(maxres,prices[i]); ans=max(ans,maxres-prices[i]); } return ans; } };

  

// Best Time to Buy and Sell Stock 2
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n=prices.size();
        res=0;
        for(int i=0;i<n-1;i++)
       {
        if(prices[i]<prices[i+1])
        res+=prices[i+1]-prices[i];
       }
       return res;

};

  

Best Time to Buy and Sell Stock系列

原文:http://www.cnblogs.com/xlqtlhx/p/7750421.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!