hot100-5盛水最多的容器
·
一、题目
长度为n的数组,找出两条线,使得他们与x轴构成的容器可以容纳最多的水。

二、思路
1、使用双指针,指向数组两端,计算面积,每次移动 高度最短的指针,试图找出最大面积。
三、代码
class Solution {
public int maxArea(int[] height) {
int i = 0, j = height.length-1;
int res = 0;
while(i<j){
int s;
if(height[i] < height[j]){
s = (j-i) * height[i];
i++;
}else{
s = (j-i) * height[j];
j--;
}
res = Math.max(res,s);
}
return res;
}
}
更多推荐


所有评论(0)