讨论/《初级算法》 - 买卖股票的最佳时机 II/
《初级算法》 - 买卖股票的最佳时机 II

c++ 求大佬指点下为什么提交会报错
class Solution {
public:
int maxProfit(vector<int>& prices) {
bool canBuy = true;
int buyDay = 0;
int total = 0;
if (prices.size() <= 0){
return total;
}
for (int i=0; i<prices.size(); i++){
if (prices[i] < prices[i+1] && canBuy){
buyDay = i;
canBuy = false;
}
if (prices[i] > prices[i+1]&& !canBuy){
total += prices[i] - prices[buyDay];
canBuy = 1;
}
}
return total;
}
};

展开全部 166 讨论