leetcode 买卖股票的最佳时机

用简单的暴力法或者一次遍历,用minprice和maxprofit记录最小买入价格以及最大收益即可。
public int maxProfit(int prices[]) { int minprice = Integer.MAX_VALUE; int maxprofit = 0; for (int i = 0; i < prices.length; i++) { if (prices[i] < minprice) { minprice = prices[i]; } else if (prices[i] - minprice > maxprofit) { maxprofit = prices[i] - minprice; } } return maxprofit; }


浙公网安备 33010602011771号