Container With Most Water

来源:互联网 发布:上海心动网络怎么样 编辑:程序博客网 时间:2024/06/01 09:12
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;    }};

0 0
原创粉丝点击