题目
代码实现:
方法一(暴力法):
class Solution {
public int maxArea(int[] height) {
int S = 0,Smax = 0;
for(int i = 0;i < height.length;i++){
for(int j = i+1;j < height.length;j++){
int y = height[i] >= height[j] ? height[j] : height[i];
S = (j-i)*y;
if(S > Smax){
Smax = S;
}
}
}
return Smax;
}
}
方法二(双指针):
class Solution {
public int maxArea(int[] height) {
int S = 0,Smax = 0,i = 0,j = height.length - 1;
while(i<j){
S = (j-i)*Math.min(height[i],height[j]);
if(S>Smax){
Smax = S;
}
if(height[i] < height[j]){
i++;
}else{
j--;
}
}
return Smax;
}
}