leetcode11盛水最多的容器
思路:
- 双指针法,一个指头一个指尾
- 无论两个指针谁向中间移动,底部的长度都会减少,那么每次移动最矮的那个指针
代码
int maxArea(vector<int>& height){
int i,j,l = height.size();
int result = 0,s=0;
for(i=0,j=l-1;i<j;){
s = (min(height[i],height[j]))*(j-i);
result = max(result,s);
if(height[i]<height[j]){
i++;
}
else{
j--;
}
}
return result;
}
还没有评论,来说两句吧...