LeetCode121双指针解法

  1. class Solution {
  2. public int maxProfit(int[] prices) {
  3.  
  4. int maxProfit=0;
  5. int minPrice=Integer.MAX_VALUE;
  6. for (int i=0;i<prices.length;i++){
  7. if (prices[i]<minPrice){
  8. minPrice=prices[i];
  9. }
  10. else if (prices[i]-minPrice>maxProfit){
  11. maxProfit=prices[i]-minPrice;
  12. }
  13.  
  14. }
  15.  
  16. return maxProfit;
  17.  
  18.  
  19. }
  20. }