11盛最多水的容器
题目链接:https://leetcode-cn.com/problems/container-with-most-water/
这道题目展示了双指针的强大,我觉得涉及到一维的问题时,就可以考虑双指针,不只是数字问题,字符串类问题也有可能有效。
class Solution {
public:
int maxArea(vector<int>& height) {
int result=0;
int i=0;
int j=height.size()-1;
while(i!=j){
if((j-i)*min(height[i],height[j])>result){
result=(j-i)*min(height[i],height[j]);
}
if(height[i]<height[j]){
++i;
}
else
--j;
}
return result;
}
};