leetcode桶装水问题
class Solution {
public:
int maxArea(vector<int>& height) {
int head=0;
int maxV=0,temp=0;
int end=height.size()-1; //记录头尾
while(head<end)
{
temp=min(height[head],height[end])*(end-head); //面积取决于短板和底的长度
maxV=max(temp,maxV); //记录临时变量与之前最大值相比取大值
if(height[head]<height[end]) //**重点***谁小谁往里缩,不可能错过最大值
head++;
else
end--;
}
return maxV;
}
};
还没有评论,来说两句吧...