题目:

  Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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的更多相关文章

  1. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  2. 【LeetCode】011 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  3. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. 【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).  ...

  5. LeetCode OJ 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  6. 【LeetCode】778. Swim in Rising Water 水位上升的泳池中游泳(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/swim-in- ...

  7. leetcode problem 11 Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  8. Leetcode Array 11 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  9. 【LeetCode】11. 盛最多水的容器

    题目 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...

随机推荐

  1. IIS SMTP status codes

    Here are the meaning of SMTP status codes. Status Code Description 211 System status, or system help ...

  2. ApiCloud重新定义移动应用开发

    http://www.apicloud.com/ 为APP开发者提供云端的API服务和数据存储服务,动态生成RESTful API,支持在线NoSQL数据表设计.API调试及用量分析:同时提供推送.云 ...

  3. flash文件制作笔记

    在uboot串口台输入printenv 可以分区以及其他信息,如下 hisilicon # printenv bootdelay=1baudrate=115200ethaddr=00:00:23:34 ...

  4. 【转】T-SQL 教程

    USE [test] GO /****** Object: StoredProcedure [dbo].[PageIndex] Script Date: 12/07/2011 10:26:36 *** ...

  5. .NET简谈插件系统开发模式

    摘选自[王清培]博客 http://www.cnblogs.com/wangiqngpei557/archive/2011/06/10/2077413.html 今天跟大家分享一下我们在日常开发中并不 ...

  6. My to do 12.25

    Merry Christmas 新的一年要来了,参加工作转眼也快半年了.回顾以往,多逢贵人.不忘初心,感慨良多.祝所有的朋友都能幸福,愿望都可以实现,日子越过越好~ Look Back 作为GH加入北 ...

  7. HDU 2087 剪花布条(KMP,不可重叠重复子串)

    给KMP传的数组一定要从0开始!! 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 这题和我上一篇博客类似,只不过不可重叠,我看了数据范围不大,所以就开 ...

  8. 有关C,C++,C#, Java的图形图像处理类库 整理(未完待续)

    1.Java相关 1.1 Jzy3D Jzy3D 是一个Java的类库,用来绘制各种各样的三维图形,如下图所示: 下载地址:jzy3d-api,官网 1.2 Proscene 是一个用于创建交互式3D ...

  9. linux命令(10)使用kill杀死含有指定关键字的进程

    命令:ps -ef|grep keyword|grep -v grep|cut -c 9-15|xargs kill -9 批量杀死包含关键字“keyword”的进程. "ps -ef&qu ...

  10. 如何刪除GitHub中的repository

    如何刪除一github中的repository,這本該是個非常簡單的操作,可一開始搜的時候,有不少文章比較含糊.這裡就記錄下來吧. 1.訪問https://github.com/settings/pr ...