码迷,mamicode.com
首页 > 编程语言 > 详细

买卖股票的最佳时机 - C++

时间:2017-03-08 23:10:34      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:for   write   c++   size   ram   ret   turn   integer   max   

class Solution {
public:
    /**
     * @param prices: Given an integer array
     * @return: Maximum profit
     */
    int maxProfit(vector<int> &prices) {
        // write your code here
        int i,j,m,n,t;
        if(prices.size()==0)
        {
            return 0;
        }
        t = prices.size();
        n=prices[0];
        m = 0;
        for(i=0;i<prices.size()-1;i++)
        {
            if(prices[i+1]<=n)
            {
                n=prices[i+1];
            }
           if(m<=prices[i+1]-n)
           {
               m=prices[i+1]-n;
           }
        }
   
        return m;
    }
};

买卖股票的最佳时机 - C++

标签:for   write   c++   size   ram   ret   turn   integer   max   

原文地址:http://www.cnblogs.com/1500802029xy/p/6523189.html

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