您的位置:首页 > 大数据 > 人工智能

Container With Most Water

2015-01-05 09:01 267 查看
class Solution {
public:
int maxArea(vector<int> &height) {
int res=0;
int head=0;
int tail=height.size()-1;
res=(tail-head)*(height[head]>height[tail]?height[tail]:height[head]);
while(head<tail)
{
if(height[head]<height[tail])
{
int tmp=height[head];
while((head<tail)&&(height[head]<=tmp))
{
head++;
}
tmp=(tail-head)*(height[head]>height[tail]?height[tail]:height[head]);
if(tmp>res)
{
res=tmp;
}
}
else
{
int tmp=height[tail];
while((head<tail)&&(height[tail]<=tmp))
{
tail--;
}
tmp=(tail-head)*(height[head]>height[tail]?height[tail]:height[head]);
if(tmp>res)
{
res=tmp;
}
}
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm leetcode