0
点赞
收藏
分享

微信扫一扫

11. 盛最多水的容器

乐百川 2022-02-22 阅读 67

题目描述:

分析:没啥可分析的。。。我的分析是错的,又麻烦而且还没法AC,我一直在想动态规划,感觉这道题会跟动态规划有关,写半天是错的,然后看了题解,居然是双指针。。。

官方双指针思路如下:

 

代码如下:

class Solution {
public:
    int maxArea(vector<int>& height) {
        // n代表height数组的长度
        int n=height.size();
        int l=0,r=n-1;
        int max=-1;
        while(l<r)
        {
            // 计算l和r围起来的面积
            int temp=min(height[l],height[r])*(r-l);
            // 更新最大值
            if(temp>max) max=temp;
            // 移动r或者l
            if(height[l]>height[r]) r--;
            else l++;
        }
        return max;
    }
};
举报

相关推荐

0 条评论