11. 盛最多水的容器

题目描述:

11. 盛最多水的容器_第1张图片

主要思路:

利用双指针的思想,向中间依次移动较低的柱子。

class Solution {
public:
    int maxArea(vector<int>& height) {
        int i=0,j=height.size()-1;
        int ans=0;
        while(i<j)
        {
            ans=max(ans,min(height[i],height[j])*(j-i));
            if(height[i]<height[j])
                ++i;
            else
                --j;
        }
        return ans;
    }
};

你可能感兴趣的:(Leetcode,leetcode)