题目链接:
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
解题思路:
1 class Solution { 2 public int maxProfit(int[] prices) { 3 int max_diff = 0; 4 int min_diff = Integer.MAX_VALUE; 5 for(int i=0;i<prices.length;i++) 6 { 7 if(prices[i]<min_diff) 8 { 9 min_diff = prices[i]; 10 } 11 12 if(max_diff<prices[i]-min_diff) 13 { 14 max_diff = prices[i]-min_diff; 15 } 16 } 17 return max_diff; 18 } 19 }