11. 盛最多水的容器(双指针)

 

   

class Solution {
    public int maxArea(int[] height) {
        int n = height.length;
        int l = 0, r = n - 1;
        int res = 0;
        while(l < r) {  // 每次移动较低的指针,这样不会丢失最大值
            res = Math.max(res,Math.min(height[l],height[r]) * (r - l));
            if(height[l] < height[r]) {
                l++;
            } else {
                r--;
            }
        }
        return res;
    }
}

 

posted @ 2020-07-08 20:39  Sexyomaru  阅读(82)  评论(0编辑  收藏  举报