11盛最多水的容器

11盛最多水的容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

public static int maxArea(int[] height) {
int res = 0;
for (int l = 0, r = height.length-1;l<r; ) {
//移动右边的面积比移动左边的面积大,那么就移动右边的否则移动左边
//计算面积
int are = Math.min(height[l], height[r]) * (r - l);
res = Math.max(are, res);
if (height[l] < height[r]) {
l++;

} else {
r--;
}
}
return res;
}