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.

代码如下:(方法一:超时)

 public class Solution {
public int maxArea(int[] height) {
int max=0;
for(int i=0;i<height.length-1;i++)
{
int min=height[i];
for(int j=i+1;j<height.length;j++)
{
if(min>height[j])
min=height[j];
if(min*(j-i)>max)
max=min*(j-i);
}
}
return max;
}
}

代码如下:(方法二:Accept)

 public class Solution {
public int maxArea(int[] height) { int left=0,right=height.length-1;
int max=Math.min(height[0],height[height.length-1])*(right-left); while(left<right)
{
if(height[left]<=height[right])
{
if(height[left]*(right-left)>max)
max=height[left]*(right-left);
left++;
}
else{
if(height[right]*(right-left)>max)
max=height[right]*(right-left);
right--;
}
}
return max;
}
}

11. Container With Most Water的更多相关文章

  1. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  2. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  3. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  4. 刷题11. Container With Most Water

    一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...

  5. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  6. [leecode]---11.container with most water

    description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...

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

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

  8. LeetCode 11. Container With Most Water (装最多水的容器)

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

  9. [LeetCode] 11. Container With Most Water 装最多水的容器

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

  10. LeetCode#11. Container With Most Water

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

随机推荐

  1. RelativeLayout练习

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  2. 转: ORACLE索引介绍和使用

    1.什么是索引 索引是建立在表的一列或多个列上的辅助对象,目的是加快访问表中的数据: Oracle存储索引的数据结构是B*树,位图索引也是如此,只不过是叶子节点不同B*数索引: 索引由根节点.分支节点 ...

  3. Oracle 11g 安装

    1.Oracle 11g安装: http://www.cnblogs.com/qianyaoyuan/archive/2013/05/05/3060471.html 2.安装完Oracle数据库,给s ...

  4. nginx查看配置文件nginx.conf路径

      当你执行 nginx -t 得时候,nginx会去测试你得配置文件得语法,并告诉你配置文件是否写得正确,同时也告诉了你配置文件得路径:  # nginx -t nginx: the configu ...

  5. 数据结构-AVL树的旋转

    http://blog.csdn.net/GabrieL1026/article/details/6311339 平衡二叉树在进行插入操作的时候可能出现不平衡的情况,AVL树即是一种自平衡的二叉树,它 ...

  6. SPOJ SUBLEX 求第k小子串

    题目大意: 对于一个给定字符串,找到其所有不同的子串中排第k小的子串 先构建后缀自动机,然后我们可以将整个后缀自动机看做是一个DAG图,那么我们先进行拓扑排序得到 *b[N] 对于每个节点记录一个sc ...

  7. Java知识结构思维导图

  8. 《day18_String练习_基本类型包装类_集合入门》

    package cn.itcast.api.String.test; public class StringTest_1 { public static void main(String[] args ...

  9. 理解Mach Port

    参考文档: 1. http://robert.sesek.com/thoughts/2012/1/debugging_mach_ports.html 2. Mach 3 Kernel Interfac ...

  10. 团队SCRUM会议(第一次)

    每日Scrum:第一天 会议时间:4.30.晚八点半 会议地点:基础教学楼一楼大厅 小组成员:郭庆樑,林彦汝,张金 团队PM:张金 会议进程 • 首先我们讨论了实验第一个Sprint1要实现的功能,我 ...