Java版代码,leetcode地址:
1 class Solution { 2 public int maxProfit(int[] prices) { 3 int min_price = Integer.MAX_VALUE; 4 int maxprofit = 0; 5 int l = prices.length; 6 for(int i = 0;i < l;i++){ 7 if(prices[i] < min_price){ 8 min_price = prices[i]; 9 }else if(prices[i] - min_price > maxprofit){ 10 maxprofit = prices[i] - min_price; 11 } 12 } 13 return maxprofit; 14 } 15 }