买卖股票的最佳时机

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;
    }
    }
};

 

posted on 2017-03-08 21:38  流-萤  阅读(135)  评论(0编辑  收藏  举报

导航