leetcode算法题--盛最多水的容器

原题链接:https://leetcode-cn.com/problems/container-with-most-water/

双指针

class Solution {
public:
    int maxArea(vector<int>& height) {
        int n = height.size();
        int res = 0;
        int l = 0, r = n - 1;
        while(l < r) {
            res = max(res, min(height[l], height[r]) * (r - l));
            (height[l] < height[r]) ? l++ : r--; // 先移动小的那个
        }
        return res;
    }    
};

你可能感兴趣的:(Algorithm,leetcode,算法,容器)