class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
int start = 0;
boolean stat = true;
for(int i=0; i<prices.length; i++){
if(i==prices.length-1){
if(stat == false){
profit += prices[prices.length-1] - prices[start];
}
}else{
if(!stat && prices[i+1]<prices[i]){
profit += prices[i]-prices[start];
stat = true;
}
if(stat && prices[i+1]>prices[i]){
start = i;
stat = false;
}
}
}
return profit;
}
}
买卖股票最佳时机
最新推荐文章于 2024-04-07 13:16:46 发布