leetcode_0011_ContainerWithMostWater 盛最多水的容器

78 阅读1分钟
/**
 * 首尾分别 2 个指针,每次移动以后都分别判断长宽的乘积是否最大。
 */
public static int maxArea(int[] height) {
    if (height == null || height.length <= 0) {
        return 0;
    }
    int left = 0;
    int right = height.length - 1;
    int ans = 0;
    while (left < right) {
        // 抓一下答案
        ans = Math.max(ans, Math.min(height[left], height[right]) * (right - left));
        // 判断一下此时的瓶颈是谁
        if (height[left] < height[right]) {
            left++;
        } else {
            right--;
        }
    }
    return ans;
}