11. 盛最多水的容器

双指针
参考题解:题解

class Solution {
    public int maxArea(int[] height) {

//双指针
   int left=0;
   int right=height.length-1;
   int max=0;
   while(left<right){
       if(height[left]<height[right]){
          max=Math.max(max,(right-left)*height[left]);
          left++;
          //如果左边比较短,那么以这个边为最左边的最大容量已经算出来的了
          //因为无论右边怎么动(不能往右动),宽度都比当前小且最高不能超过heght[left]了

       }
       else{
           max=Math.max(max,(right-left)*height[right]);
           right--;
       }
   }
   return max;
    }
}
posted @ 2021-06-06 21:56  wsshub  阅读(35)  评论(0)    收藏  举报