383.Container With Most Water-装最多水的容器(中等题)
来源:互联网 发布:花生壳linux 编辑:程序博客网 时间:2024/05/20 09:08
装最多水的容器
题目
给定 n 个非负整数 a1, a2, …, an, 每个数代表了坐标中的一个点 (i, ai)。画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0)。找到两条线,使得其与 x 轴共同构成一个容器,以容纳最多水。
注意事项
容器不可倾斜。样例
给出[1,3,2], 最大的储水面积是2.
题解
首先理解题意,两条垂直线和X轴组成的容器的储水面积S=Math.min(heights[left],heights[right])*(right-left)。可以通过首尾双指针相向遍历,每次移动值较小的那个指针,直至两指针相遇。
public class Solution { /** * @param heights: an array of integers * @return: an integer */ public int maxArea(int[] heights) { int max = 0; int left = 0; int right = heights.length-1; while (left < right) { max = Math.max(max,Math.min(heights[left],heights[right])*(right-left)); if (heights[left] < heights[right]) { left++; } else { right--; } } return max; }}
Last Update 2016.9.23
0 0
- 383.Container With Most Water-装最多水的容器(中等题)
- 383.Container With Most Water-装最多水的容器(中等题)
- Container With Most Water(能装最多水的容器)
- Container With Most Water --装最多水的容器(重)
- LeetCode OJ 之 Container With Most Water(能装最多水的容器)
- Leetcode刷题记——11. Container With Most Water(装最多水的容器)
- 6.container-with-most-water(装最多水的容器)
- Container With Most Water (容器中盛最多的水)
- 含水最多的容器(Container With Most Water)
- LeetCode | Container with most water(装最多的水)
- 11. Container With Most Water(求能装最多水的容器)
- Container With Most Water 装最多的水
- LeetCode 11. Container With Most Water(容器装水)
- Container With Most Water 求容器里能放最多的水 @LeetCode
- LeetCode11. Container With Most Water(思维题:选择左右边使得容器所盛水最多)
- Container With Most Water 盛最多的水
- Container With Most Water容器盛水
- Container With Most Water(最大容器)
- jQuery选择器学习小结
- 芯片内上拉和下拉电阻的作用
- win7下安装使用git以及上传本地代码到gitHub上
- 树及其操作(C语言版)
- 浅析pthread_cond_wait
- 383.Container With Most Water-装最多水的容器(中等题)
- DES加解密
- iOS MJRefresh源码研读
- ThinkPHP 读取后台的数据库
- Java heap space
- Echarts学习笔记~
- 使用OpenGL显示双目视觉三维重构效果
- 跑步的正确姿势
- 明白了这些,我才开始月入10W