class Solution {
public int maxProfit(int[] prices) {
int res = 0;
for(int i = 0;i < prices.length -1;i++){
if(prices[i] < prices[i + 1] ) res += (prices[i+1] - prices[i]);
}
return res;
}
}
因为没有手续费,只收集每天的正利润即可
本文探讨了一种简单的算法,Solution类中的maxProfit方法,用于在没有手续费的情况下,计算连续持有股票的最高利润。通过遍历价格数组,捕捉价格上升带来的每日收益。适合初学者理解买卖股票时机的数学模型。
415

被折叠的 条评论
为什么被折叠?



