leetcode刷题目 总结 记录 备忘11

来源:互联网 发布:捕鱼达人软件出售 编辑:程序博客网 时间:2024/06/05 00:24

leetcode11

Container With Most Water

 

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container.

从两头开始 ,先求一次面积,两边下标差乘两者之间的最低值,即短板,之后开始遍历,数值小的一方往里走,因为是短板,所以得自己往里走,以期能找到一个让对面的数值成为小值的数,才有可能取得最大的面积,之后继续计算面积,与之前的面积相比,替换小的,完成遍历即可。原谅我的语言表述能力较弱。

class Solution {public:    int maxArea(vector<int>& height) {        int l = 0, r = height.size() - 1;        int max = 0;                while (l < r)        {            int temp = (r-l) * min(height[l], height[r]);            if (temp > max)            max = temp;                        if (height[l] <= height[r])            l++;            else            r--;        }                return max;    }};


0 0
原创粉丝点击