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

买卖股票的最佳时机

时间:2017-03-08 23:02:18      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:length   array   integer   turn   时机   else   log   int   ram   

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

 

买卖股票的最佳时机

标签:length   array   integer   turn   时机   else   log   int   ram   

原文地址:http://www.cnblogs.com/t-p-h/p/6523206.html

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