题解 | #接雨水问题#
盛水最多的容器
http://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47
双指针
class Solution {
public:
int maxArea(vector<int>& height) {
// write code here
//定义变量初始化
int left = 0,right = height.size()-1;
int Maxleft = 0,Maxright = 0;
int ans = 0;
//结束条件为左右指针相遇
while(left < right){
//更新左右的最大值,我们只需要左右两边较小的那个最大值
Maxleft = max(Maxleft,height[left]);
Maxright = max(Maxright,height[right]);
//更新最大容器ans
if(Maxleft < Maxright){
ans = max(ans,(right-left)*Maxleft);
left++;
}else{
ans = max(ans,(right-left)*Maxright);
right--;
}
}
return ans;
}
};