您的位置:首页 > 其它

LeetCode_best-time-to-buy-and-sell-stock

2015-09-15 19:55 375 查看
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if(len == 0)
return 0;
int profit = 0,low = prices[0];//low记录当前最小的股价
for(int i = 1;i < len;i++){
if(prices[i] < low)//一旦发现更小的股价,更新
low = prices[i];
else
if(prices[i] - low > profit)
profit = prices[i] - low;
}
return profit;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: