我的做法:使用一个数组装载可能的结果,再排序后输出最大值。
class Solution {
public int maxArea(int[] height) {
int l = 0;
int r = height.length - 1;
int[] sum = new int[height.length];
int index = 0;
while (l<r){
if(height[r] >= height[l]){
sum[index] = height[l]*(r-l);
index ++;
l++;
}else if(height[r] < height[l]){
sum[index] = height[r]*(r-l);
index ++;
r--;
}
}
Arrays.sort(sum);
return sum[sum.length-1];
}
}