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. Android webview 取得javascript返回值

    package com.she.jyass.UI; import android.content.Context; import android.webkit.WebView; public clas ...

  2. ACM心情总结

    已经快要12点了,然而还有5000字概率论论文没有动.在论文里,我本来是想要总结一下ACM竞赛中出现过的概率论题目,然而当敲打第一段前言的时候,我就迟疑了. 我问自己,ACM竞赛到底有什么现实意义. ...

  3. 在Linux中查看文件的编码及对文件进行编码转换

    如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8.下面介绍一下,在Li ...

  4. C#解析复杂的Json成Dictionary<key,value>并保存到数据库(多方法解析Json 四)

    准备工作: 1.添加引用System.Web.Extensions, 2..net3.5+版本都有,如果VS2010找不到,在这个文件夹找:C:\Program Files\Reference Ass ...

  5. format 字符串

    http://www.cnblogs.com/mumble/archive/2011/05/25/2056462.html ShowMessage( Format('this is %.0f',[ t ...

  6. [USACO精选] 第三章 排序

    #9 重排干草 2014-02-12 QAQ这么快居然开学了,这么来说时间越来越少了…开学第二天,作业不多,赶紧抽出时间把这道想了很久的题给搞定……真的想了很久,其实看了解题也想了很久,我觉得我等数学 ...

  7. Program A-归并排序

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  8. 踏着前人的脚印学hadoop——ipc中的Server

    1.An abstract IPC service.  IPC calls take a single {@link Writable} as a parameter, and return a {@ ...

  9. 记一个有想法却没能力实现的硬件产品——mp3校时闹钟

    枕头旁的闹钟,我想大家都用过,很便宜.用一节干电池供电.但其最大的缺点就是不太准,不能校时. 电池啥事用光,也不知道.钟是走的很慢,没按时闹,搞的自己迟了到. 于是就有了我的漫长思考过程... 先说手 ...

  10. 跨域请求之jQuery的ajax jsonp的使用解惑

    前天在项目中写的一个ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法,直接执行了error方法提示错误——ajax jsonp之前并没有用过,对其的理解为跟普 ...