设置个最大最小值然后进行比较完事。java的max值是Integer.MAX_VALUE。
class Solution {
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE, maxProfit = 0;
for (int price: prices) {
maxProfit = Math.max(maxProfit, price - minPrice);
minPrice = Math.min(price, minPrice);
}
return maxProfit;
}
}
402

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



