> 文档中心 > 【从0到1冲刺蓝桥杯国赛】每日一练——盛最多水的容器(双指针)

【从0到1冲刺蓝桥杯国赛】每日一练——盛最多水的容器(双指针)

最多水的容器icon-default.png?t=M276https://leetcode-cn.com/problems/container-with-most-water/

题目描述:

 

 思路分析: 

本题采用双指针,看代码就可以知道思路

C++实现: 

class Solution {public:    int maxArea(vector& height) { int l = 0, r = height.size() - 1; int ans = 0; while(l < r){     int area = min(height[l],height[r]) * (r - l);     ans = max(ans, area);     if(height[l] <= height[r]){  ++l;     }     else{  --r;     } } return ans;    }};