首页 > 代码库 > 买卖股票最佳时机

买卖股票最佳时机

class Solution {
public:
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
int maxProfit(vector<int> &prices) {
// write your code here
if(prices.size() == 0){
return 0;
}

int max = 0;
int cur = prices[0];
for(int j = 0; j < prices.size(); ++j){
if(prices[j] < cur){
cur = prices[j];
}else{
int tmp = prices[j] - cur;
if(tmp > max){
max = tmp;
}
}
}
return max;
}
};

买卖股票最佳时机