11. Container With Most Water

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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     }

 

以上是关于11. Container With Most Water的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode11-Container With Most Water

leetcode 11. Container With Most Water

11. Container With Most Water

11. Container With Most Water

LeetCode11. Container With Most Water 解题报告

11. Container With Most Water