Leetcode02 买卖股票最佳时期II
方法:简单的一次遍历
- 遍历输入的股价
若第i+1天的价格比第i天的价格高,那么我们就将第i+1天与第i天的价格差计入到我们的总利润中,直到列表遍历结束。
class Solution{
public:
int maxProfit(vector<int>& prices){
int profit = 0;
for(int i = 0; i < prices.size(); i++)
{
if (prices[i+1] > prices[i])
{
profit += prices[i+1] - prices[i];
}
return profit;
}
}
};