11. 盛最多水的容器

11.盛最多水的容器

题目链接

11. 盛最多水的容器

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int maxArea(vector<int>& height) {
//双指针;
int ans =-1;
int left=0;
int right=height.size()-1;
while(left<right){
int temp=0;
if(height[left]<=height[right]){
temp = height[left]*(right-left);
ans = std::max(ans,temp);
left++;
}else{
temp = height[right]*(right-left);
ans = std::max(ans,temp);
right--;
}

}
return ans;

}
};

参考资料

盛最多水的容器 - 盛最多水的容器 - 力扣(LeetCode)


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!