买卖股票的最佳时机
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; } };