巩长胜

导航

买卖股票的最佳时机

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

posted on 2017-03-08 20:03  苏莫  阅读(81)  评论(0编辑  收藏  举报