题解 | #买卖股票的最好时机(一)#
买卖股票的最好时机(一)
http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec
class Solution {
public:
/**
*
* @param prices int整型vector
* @return int整型
*/
int maxProfit(vector<int>& prices) {
int mn = prices[0];
int ans = 0;
for(int i=1; i<prices.size(); i++){
if(prices[i] < mn){
mn = prices[i];
} else {
ans = max(ans, prices[i] - mn);
}
}
return ans;
// write code here
}
};
查看19道真题和解析