【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.
翻译:
给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai)。 n个垂直线段例如线段的两个端点在(i,ai)和(i,0)。 找到两个线段,与x轴形成一个容器,使其包含最多的水。
备注:不考虑倾斜容器。
思路:容积其实就是面积,我们都知道长方形的面积=长*宽,不妨我们就从长最长的长方形找起,即令left = 0, right = height.size() - 1,但是在找下一个长方形时,长肯定会变短,要弥补这一段损失就必须加宽宽度,所以一下个就换掉两条宽中较小的那一个。
public class Solution {
public int maxArea(int[] height) {
int left=0;
int right=height.length-1;
int maxArea=0;
while(left<right){
maxArea=Math.max(maxArea,Math.min(height[left],height[right])*(right-left));
if(height[left]>height[right]){
right--;
}else{
left++;
}
}
return maxArea;
}
}
【LeetCode】11. Container With Most Water的更多相关文章
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- 【LeetCode】011 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode two_pointer】11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode OJ 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 【LeetCode】778. Swim in Rising Water 水位上升的泳池中游泳(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/swim-in- ...
- leetcode problem 11 Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Leetcode Array 11 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 【LeetCode】11. 盛最多水的容器
题目 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...
随机推荐
- Tomcat在eclipse中起动成功,主页却打不开
症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他项目页面也不能访问. 关闭eclipse里面的tomc ...
- foxmail 6.5升级到7.0版本后,旧邮件的导入处理
随着foxmail 7.0版的火热升级,部分从foxmial 6.5版升级到7.0版的用户可能会出现旧邮件丢失的困扰.这里,foxmail为大家提供的解决方案如下: 打开Foxmail,点击 文件 ...
- MyEclipse9中的不伤眼修改、FreeMarker插件、JQuery提示插件、全屏(FullScreen)插件的安装
============下载相关附件===================== http://files.cnblogs.com/fhtwins/eclipse-fullscreen_1.0.7.zi ...
- pthread_key_t和pthread_key_create()详解
pthread_key_t和pthread_key_create()详解 下面说一下线程中特有的线程存储, Thread Specific Data .线程存储有什么用了?他是什么意思了?大家都知道, ...
- kafka模拟客户端发送、接受消息
producer 消息的生成者,即发布消息 consumer 消息的消费者,即订阅消息 broker Kafka以集群的方式运行,可以由一个或多个服务组成,服务即broker zook ...
- Oracle11G登录时提示:ORA-12557: TNS:协议适配器不可加载
原文 Oracle11G登录时提示:ORA-12557: TNS:协议适配器不可加载 初步分析是ORACLE_HOME设置错误引起的.前几天不小心看到系统环境变量中的其值为空,就手贱的加载了一个ora ...
- [物理学与PDEs]书中的错误指出
记号意义: P--Page, 第几页; L--Line, 顺数第几行; LL--Last Line, 倒数第几行. P 64 L 1 ``15)'' should be ``14)''. P 70 L ...
- ajax提交后完全不进入action直接返回错误
今天遇到个问题就是jQuery提交ajax请求,居然没有进入action的断点而直接返回错误信息. 仔细排查后才发现原来是因为客户端提交的某字段是100w的值,而后台对应的字段是个Short类型,根本 ...
- get/close not same thread Druid 连接池一个设置
我就郁闷了,1000W+数据审核每次总是到一半就出这么个错,仔细找找原来是一个配置项的小问题,removeAbandonedTimeout 这个代表你从连接池取出一个连接多少秒之后你还没还回来,那就强 ...
- 通过位运算生成ID
public static void main(String[] args) { long serverId = 65535; System.out.println("ServerId:&q ...