题目链接:https://leetcode-cn.com/problems/container-with-most-water/
题目如下:
class Solution {
public int maxArea(int[] height) {
int i=0,j=height.length-1;
int capacity=0,max=0;
int h1,pos;//pos用于标记下一次应该往里缩的是哪边
while(i<j){
//两边往中间依次缩短(双指针法)
//因为长度在缩小,故此,目标是希望能得到尽量的高的高度
if(height[i]>height[j]) {h1=height[j];pos=1;}
else {h1=height[i];pos=0;}
capacity=(j-i)*h1;
if(capacity>max) max=capacity;
if(pos==0) i++;
else j--;
}
return max;
}
}