首页 > 其他 > 详细

leetcode Container With Most Water

时间:2015-03-28 20:20:17      阅读:105      评论:0      收藏:0      [点我收藏+]

代码:

#include<iostream>
#include<vector>

using namespace std;

int maxArea(vector<int> &height)
{
    int L = height.size();
    int i = 0;
    int j = L - 1;
    int max = 0;
    int left = height[i];
    int right = height[j];
    while (i < j)
    {
        int area;
        if (height[i] < height[j])
        {
            area = height[i] * (j - i);
            while (height[++i] <= left)
                ;
            left = i;
        }
        else
        {
            area = height[j] * (j - i);
            while (height[--j] <= right)
                ;
            right = height[j];
        }
        if (area > max)
            max = area;
    }
    return max;
}

int main()
{
    vector<int> height = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
    cout << maxArea(height) << endl;
}

 

leetcode Container With Most Water

原文:http://www.cnblogs.com/chaiwentao/p/4374677.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!