【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).找出其中的两 ...
 
随机推荐
- IntelliJ IDEA添加过滤文件或目录
			
Settings→Editor→File Types 在下方的忽略文件和目录(Ignore files and folders)中添加自己需要过滤的内容 下图为我自己添加过滤的内容,例如:*.iml; ...
 - C语言每日一题之No.9
			
再做决定之前,我还是做好自己该做的.我不希望几年后会悔恨自己为什么在最该努力的时候不愿意吃苦.尊敬的女王陛下,请接题: 一.题目:有已按升序排好顺序的字符串a,编写程序将字符串s中的每个字符按升序的规 ...
 - 【MySQL】原理 之 事务
			
1.MySQL逻辑架构大致分为:连接认证层,核心服务层,存储引擎层. 2.锁策略,需要在开销和数据的安全性之间寻求平衡,这种平衡会影响到性能. 3.写锁优先于读锁. 4.行级锁只在存储引擎层实现,而M ...
 - Hadoop学习5--配置本地开发环境(Windows+Eclipse)
			
一.导入hadoop插件到eclipse 插件名称:hadoop-eclipse-plugin-2.7.0.jar 我是从网上下载的,还可以自己编译. 放到eclipse安装目录下的plugins文件 ...
 - 四层LB和七层LB
			
总结: 基于MAC地址玩的是二层(虚拟MAC地址接收请求,然后再分配到真实的MAC地址), 基于IP地址玩的是三层(虚拟IP地址接收请求,然后再分配到真实的IP地址), 基于IP地 ...
 - jstl标签库基础教程及其使用代码
			
概述 在 JSP 页面中,使用标签库代替传统的 Java 片段语言来实现页面的显示逻辑已经不是新技术了,然而,由自定义标签很容易造成重复定义和非标准的实现.鉴于此,出现了 JSTL ( JSP Sta ...
 - LR11
			
HP LoadRunner Readme for the Windows operating system Software version: 11.00 Publication date: Octo ...
 - 深入分析ConcurrentHashMap(转)
			
线程不安全的HashMap 因为多线程环境下,使用HashMap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap,如以下代码 final HashMap ...
 - RocketMQ术语[转]
			
RocketMQ RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点:能够保证严格的消息顺序 能够保证严格的消息顺序 提供丰富的消息拉取模式 高效的订阅者水平扩展能力 实时的消息订阅机制 ...
 - ajax实现的无刷新分页代码实例
			
一.html代码部分: <table class="table style-5"> <thead id="t_head"> ...