美文网首页
leetcode 11. 盛最多水的容器

leetcode 11. 盛最多水的容器

作者: Source_Chang | 来源:发表于2020-10-20 12:59 被阅读0次

leetcode

核心思想:双指针,数字小的那个指针移动

class Solution {
public:
    int maxArea(vector<int>& height) {

        int left = 0;
        int right = height.size() - 1;
        int maxArea = 0;
        while ( left < right ) {
            
            maxArea = max( min( height[left], height[right] ) * ( right - left ), maxArea );
            if ( height[left] <= height[right] ) {

                ++left;

            } else {

                --right;
            }
        }

        return maxArea;
    }
};

相关文章

网友评论

      本文标题:leetcode 11. 盛最多水的容器

      本文链接:https://www.haomeiwen.com/subject/jdwkmktx.html