装最多水的容器
来源:互联网 发布:软件版权声明 编辑:程序博客网 时间:2024/05/16 03:41
描述
给定 n 个非负整数 a1, a2, …, an, 每个数代表了坐标中的一个点 (i, ai)。画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0)。找到两条线,使得其与 x 轴共同构成一个容器,以容纳最多水。
注意事项
容器不可倾斜。
样例
给出[1,3,2], 最大的储水面积是2.
思考
- 头尾指针遍历,保存最大值
代码
// By Lentitudeclass Solution {public: /** * @param heights: a vector of integers * @return: an integer */ int maxArea(vector<int> &heights) { // write your code here if (heights.size() < 2) return NULL; int ret = 0; int r = heights.size()-1; int l = 0; while (l < r) { ret = max(ret,min(heights[l],heights[r])*(r-l)); if(heights[l]<heights[r]) l++; else r--; } return ret; }};
0 0
- 装最多水的容器
- 装最多水的容器
- 装最多水的容器
- 装最多水的容器
- 装最多水的容器
- LintCode-装最多水的容器
- lintcode-装最多水的容器-383
- LintCode_383_装最多水的容器
- LintCode:装最多水的容器
- lintcode,装最多水的容器
- Lintcode 装最多水的容器
- Java实现-装最多水的容器
- Lintcode 装最多水的容器
- LintCode:M-装最多水的容器
- 装最多水的容器-LintCode
- Container With Most Water(能装最多水的容器)
- lintcode刷题——装最多水的容器
- [LintCode 383] 装最多水的容器(Python)
- 【LeetCode】之字形变换
- 应用程序多语言设置
- jquery内外部插入
- js检测文件类型
- (转)华兴资本包凡:我们这么屌,是有原因的
- 装最多水的容器
- 170411 VJ题解(2)(ZOJ
- 谷歌Inception网络中的Inception-V3到Inception-V4具体作了哪些优化?
- 51nod 1270 数组的最大代价
- 关于去除表单选中时的蓝色边框和设置内置提示字体
- 10-进程间通信-有名管道FIFO(功能:传输数据)
- HDU 2082 找单词 母函数
- JSON和GSON学习
- Bean named 'transactionAdvice' must be of type [org.aopalliance.aop.Advice], but was actually of typ