Leetcode02 买卖股票最佳时期II![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/759f5f92e25f3eb1e30726f916a06199.png)
方法:简单的一次遍历
- 遍历输入的股价
若第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;
}
}
};