public class Solution { public int maxProfit(int[] prices) { if(prices == null || prices.length == 0) return 0; int minPrice = prices[0]; //记录最低价 int maxProfit = 0; //记录最大收益 for(int i = 1; i < prices.length; i++){ if(prices[i] < minPrice){ //如果当前价低于最低价,更新最低价 minPrice = prices[i]; }else if(prices[i] - minPrice > maxProfit){ //如果当前价高于最低价,并且当前收益高于最大收益,更新最大收益 maxProfit = prices[i] - minPrice; } } return maxProfit; } }

股票最大利润:Java 代码实现

原文地址: https://www.cveoy.top/t/topic/ozkK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录