【leetcode】【11】Container With Most Water
来源:互联网 发布:淘宝一般违规48分申诉 编辑:程序博客网 时间:2024/05/17 05:13
一、问题描述
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) 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.
二、问题分析
这是一维数组的遍历问题。而再处理遍历的时候双指针是非常常用的方法。该题显然也需要一左一右两个指针来遍历。还有一点需要明确的就是短板决定容器的容量。
三、Java AC代码
public int maxArea(int[] height) {int max = 0;if (height == null || height.length < 2) {return max;}int left = 0, right = height.length - 1;while (left < right) {int tmp = Math.min(height[left], height[right]) * (right - left);if (tmp > max) {max = tmp;}if (height[right]<height[left]) {int key = height[right];while(--right > left && height[right] < key);}else {int key = height[left];while(++left < right && height[left] < key);}}return max;}
0 0
- LeetCode 11: Container With Most Water
- leetcode 11 Container With Most Water
- Leetcode【11】:Container With Most Water
- [leetcode 11] Container With Most Water
- [leetcode 11] Container With Most Water
- LeetCode | #11 Container With Most Water
- leetcode.11---------Container With Most Water
- [LeetCode 11]Container With Most Water
- leetcode 11 Container With Most Water
- leetcode-11 Container With Most Water
- Container With Most Water - LeetCode 11
- [Leetcode 11, Medium] Container With Most Water
- LeetCode 11 - Container With Most Water
- leetcode 11 -- Container With Most Water
- #LeetCode OJ #11 Container With Most Water
- Leetcode #11 Container With Most Water
- leetcode-11Container With Most Water
- leetcode 11 -- Container With Most Water
- springmvc4 mybatis 整合 框架源码
- ecneuqeSevitucesnoCtsegnoL.128
- Java中的向前引用
- Java反射机制梳理(一)
- BZOJ2657: [Zjoi2012]旅游(journey)
- 【leetcode】【11】Container With Most Water
- UIView中hidden、alpha、clear color与opaque的区别
- python自动给数字前面补0的方法
- Jenkins修改时区(Docker)
- The identity used to sign the executable is no longer valid.Please verify that your device’s clock i
- fscanf()函数详解
- 并发Concurrent与并行Parallel的区别
- 单任务计算机是怎样工作的————(以一段汇编代码执行过程为例)
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别