Java [leetcode 11] Container With Most Water
问题描述:
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.
解题思路:
这道问题还是比较简单的,首先要理解它的意思,整个容器的高度是由较短的那根线决定的。然后从两边分别开始往中间收缩,对短的那根线进行往里收缩。这是容器的高度是由短的那根线决定的,那么当对高线往中间收缩时候,面积是在逐渐减小的,所以应该对矮的那根线进行收缩。
代码如下:
public class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int maxArea = (right - left) * Math.min(height[left], height[right]);
int temp;
while(left < right){
if(height[left] < height[right]){
left++;
}
else{
right--;
}
temp = (right - left) * Math.min(height[left], height[right]);
if(temp > maxArea)
maxArea = temp;
}
return maxArea;
}
}
Java [leetcode 11] Container With Most Water的更多相关文章
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- 蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]
题目 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- C#解leetcode 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water My Submissions Question 解题思路
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- Python学习_数据排序方法
Python对数据排序又两种方法: 1. 原地排序:采用sort()方法,按照指定的顺序排列数据后用排序后的数据替换原来的数据(原来的顺序丢失),如: >>> data1=[4,2, ...
- 【BZOJ 1040】 [ZJOI2008]骑士
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...
- Struts 2 Tutorials
http://www.dzone.com/tutorials/java/struts-2/struts-2-tutorial/struts-2-tutorial.html http://struts. ...
- 第一个js面向对象的小实验
$.extend({ cal: function (num1,num2,oper,aftercal) { this.n1 = num1; ...
- 如何让VS根据编译环境选择相应的配置文件
其实微软还是蛮有创造力的,一个配置文件居然弄了这么多的形式,从原来的ini到现在的xml,总而言之让我们这些在微软殿堂里的程序员翘着屁股追赶. 微软最新的配置文件实际上就是个xml文件,以后缀名.co ...
- 分享SCI写作经验和一些工具
- Codeforces Round #243 (Div. 2) A~C
题目链接 A. Sereja and Mugs time limit per test:1 secondmemory limit per test:256 megabytesinput:standar ...
- linux网络环境配置
第一种方法: (red hat) (1)用root身份登录,运行setup命令进入到text mode setup utility 对网络进行配置,这里可以进行ip,子网掩码,默认网关,dns的设置. ...
- Web Api 入门
http://www.cnblogs.com/developersupport/p/WebAPI-Security.html http://www.cnblogs.com/r01cn/archive/ ...
- 阿里云 EDAS-HSF 用户指南
阿里云 EDAS-HSF 用户指南 针对 EDAS v2.3.0©Alibaba EDAS 项目组2015/8/19 1 前言本文档旨在描述阿里云 EDAS 产品中应用服务化模块的基本概念,以及如何使 ...