盛水最多的容器-双指针

63 阅读1分钟
// 盛水最多的容器-双指针  
// 输入:[1,8,6,2,5,4,8,3,7]  
// 输出:49  
// 解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。  
public static int maxArea(int[] height){  
    int i=0,j=height.length-1,res=0;  
    while (i<j){  
        res=height[i]<height[j]?Math.max(res,(j-i)*height[i++]):  
        Math.max(res,(j-i)*height[j--]);  
    }  
    return res;  
}