code
class Solution {
public:
int maxProfit(vector<int>& prices) {
//1 <= prices.length <= 3 * 104
//累加正利润
int sum = 0;
for(int i = 1;i<prices.size();i++){
if(prices[i]>prices[i-1])
{
sum +=prices[i]-prices[i-1];
}
}
return sum;
}
};
注意数组下表我们应该从i和i-1判断,如果判断i和i+1就会有越界问题出现!