11. Container With Most Water
用two pointers夹逼的方法,刚开始把left和right设置在数组的两端,如果left处比right处矮,那么说明left处是瓶颈,否则就是right处是。
同时维护一个全局的maxArea
1 public int maxArea(int[] height) { 2 if(height == null || height.length < 2) { 3 return 0; 4 } 5 int maxArea = 0; 6 int len = height.length; 7 int right = len - 1; 8 int left = 0; 9 while(left < right) { 10 int area = (right - left) * Math.min(height[left], height[right]); 11 maxArea = Math.max(maxArea, area); 12 if(height[left] < height[right]) { 13 left++; 14 } else { 15 right--; 16 } 17 } 18 return maxArea; 19 }