买卖股票的最佳时机

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

 

posted @ 2017-03-06 21:24  daifengjiao  阅读(133)  评论(0编辑  收藏  举报