给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。说明:你不能倾斜容器。(自己学习写了解析)
class Solution {
public int maxArea(int[] height) {
int i=0; //指针1,从左往右走
int j=height.length-1; //指针2,从右往左走
int res=0; //最大面积
while (i<j){ //两指针未相见的时候
res=height[i]<height[j]?Math.max(res, (j - i) * height[i++]):Math.max(res, (j - i) * height[j--]);
} //判断heigh[i]和heigh[j],谁小就取谁,然后拿去与两个下标差
return res;
}
}