Container With Most Water(C++盛最多水的容器)

Myth丶恋晨 2022-10-09 03:10 58阅读 0赞

(1)双指针,夹逼法

  1. class Solution {
  2. public:
  3. int maxArea(vector<int>& height) {
  4. int n=height.size(),maxs=0;
  5. int i=0,j=n-1,temp=0;
  6. while(i!=j) {
  7. temp=(j-i)*min(height[i],height[j]);
  8. if(maxs<temp) maxs=temp;
  9. if(height[i]<height[j]) i++;
  10. else j--;
  11. }
  12. return maxs;
  13. }
  14. };

发表评论

表情:
评论列表 (有 0 条评论,58人围观)

还没有评论,来说两句吧...

相关阅读