点击打开链接
class Solution { public: int maxProfit(vector<int>& prices) { if(prices.size() <=1) return 0; int low = prices[0], maxProfit =0; for(int i=1; i<prices.size(); i++){ int profit = prices[i] - low; if(maxProfit < profit) maxProfit = profit; if(prices[i] < low) low = prices[i]; } return maxProfit; } };