面试经典 150 题 4 —(双指针)— 11. 盛最多水的容器

11. 盛最多水的容器

面试经典 150 题 4 —(双指针)— 11. 盛最多水的容器_第1张图片

方法一
class Solution {
public:
    int maxArea(vector<int>& height) {
        int maxA = 0,left = 0, right = height.size()-1;
        while(left < right){
            int area = (right - left) * min(height[left],height[right]);
            if(area >= maxA){
                maxA = area;
            }
            if(height[left] >= height[right]){
                right--;
            }
            else{
                left++;
            }
        }
        return maxA;
    }
};
class Solution {
public:
    int maxArea(vector<int>& height) {
        int maxA = 0,left = 0, right = height.size()-1;
        while(left < right){
            maxA = max(maxA,(right - left) * min(height[left],height[right]));

            if(height[left] >= height[right]){
                right--;
            }
            else{
                left++;
            }
        }
        return maxA;
    }
};

你可能感兴趣的:(leetcode,leetcode,c++,算法)