0%

11题,盛最多水容器

答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @author xixing
* @version 1.0
* @date 2020/2/20 17:02
*/
public class N11 {
public int maxArea(int[] height) {
int area=0;
int i=0,j=height.length-1;
while (i!=j){
if(height[i]>height[j]){
int temp=(j-i)*height[j];
j--;
area=Math.max(temp,area);
}else {
int temp=(j-i)*height[i];
i++;
area=Math.max(temp,area);
}
}
return area;
}

}

题解

双向链表,由于水的盛水容量是由短板决定的,所以哪边是短板就移动,这样才有可能使得盛水容量变大。