【从0到1冲刺蓝桥杯国赛】每日一练——盛最多水的容器(双指针)
盛最多水的容器https://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; }};