Leetcode 11. 盛最多水的容器-双指针

 

代码:

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

    }
};

 

posted @ 2021-10-26 17:59  A-inspire  Views(22)  Comments(0Edit  收藏  举报