LeetCode11. 盛最多水的容器(双指针)

https://leetcode-cn.com/problems/container-with-most-water/

class Solution {
public:
    int maxArea(vector<int>& height) {
        int left = 0;
        int right = height.size() - 1;
        int res = 0;
        while(left < right) {
            if(height[left] <= height[right]) {
                int temp = height[left] * (right - left);
                res = max(res, temp);
                left++;
            }else {
                int temp = height[right] * (right - left);
                res = max(res, temp);
                right--;
            }
        }
        return res;
    }
};
发布了8 篇原创文章 · 获赞 0 · 访问量 117

猜你喜欢

转载自blog.csdn.net/oykotxuyang/article/details/105599343