leetcode 11.Container With Most Water

2022-06-23 13:59:39 浏览数 (1)

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

这道题采用对撞指针的思路,从两侧向中间逼近,逼近的过程中,每次选择较低的一面墙

代码语言:javascript复制
class Solution {
public:
    int maxArea(vector<int>& height) {
        auto lp = height.begin();
        auto rp = height.end()-1;
        int res = 0;
        int area = 0;
        while(lp!=rp){
            area = min((*lp),(*rp))*(distance(lp,rp));
            res = max(res,area);
            if(*lp<*rp){
                lp  ;
            }
            else{
                rp--;
            }
        }
        
        return res;
    }
};

0 人点赞