题目链接:https://leetcode.com/problems/container-with-most-water/description/

题目大意:给出一串数组(a1, a2, a3, ...an),表示坐标(i, ai),同样表示一条直线是从(i, 0)到(i, ai),从中选出两条直线,计算其容器能蓄水多少,并找出最多的蓄水量。(容器不能倾斜,就是两条直线可以是不同高度,但是蓄水量要按照低高度的那条直线来计算),例子如下:

法一:暴力,对于一条直线,分别遍历左边和右边直线,计算其蓄水量,取最大者。可惜超时了。代码如下:

     public int maxArea(int[] height) {
int res = 0;
for(int i = 0; i < height.length; i++) {
//计算左边直线
for(int j = height.length - 1; j > i; j--) {
if(height[j] >= height[i]) {
res = Math.max(res, (j - i) * height[i]);
break;
}
}
//计算右边直线
for(int j = 0; j < i; j++) {
if(height[j] >= height[i]) {
res = Math.max(res, (i - j) * height[i]);
break;
}
}
}
return res;
}

法二:两指针移动,与42题的两指针移动类似但略有区别,左指针与右指针相比较,记录较大者,若左指针较小,则从左往右移动,若右指针较小,则从右往左移动,如果当前值比较大者大,则计算:(较大者下标-当前值下标)*当前值高度,计算得到最后的结果。代码如下(耗时8ms):

     public int maxArea(int[] height) {
int res = 0, left = 0, right = height.length - 1;
while(left < right) {
//如果左指针<=右指针
if(height[left] <= height[right]) {
while(left < right && height[left] <= height[right]) {
res = Math.max(res, (right - left) * height[left++]);
}
}
//如果左指针>右指针
else {
while(left < right && height[left] > height[right]) {
res = Math.max(res, (right - left) * height[right--]);
}
}
}
return res;
}

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 Array Medium 11. Container With Most Water

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

  3. 刷题11. Container With Most Water

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

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

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

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

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

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

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

  7. [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: 找出数组中最大的数 ...

  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 problem 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, ai).  ...

随机推荐

  1. ZOJ3067_Nim

    题目的意思就不说了,典型的取石子的博弈问题. 题目的前半部分就是赤果果的SG函数值异或就可以了,其中Sg函数值就是石子数本身. 但是接下来有个小变换,就是要你输出先手必胜有多少种不同的取法. 首先要想 ...

  2. 使用android资源

    1.我们可以命名的资源种类有多少? 答: res/anim/ XML文件,它们被编译进逐帧动画(frame by frame animation)或补间动画(tweened animation)对象 ...

  3. Linux内核设计第五周学习总结 分析system_call中断处理过程

    陈巧然原创作品 转载请注明出处   <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 使用gdb跟踪分析一 ...

  4. 电子商务(电销)平台中内容模块(Content)数据库设计明细

    以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 文章表 (article)|-- 自动编号|-- 文章标题 (title)|-- 文章类别编号 (c ...

  5. 在 Ubuntu16.04上安装anaconda+Spyder+TensorFlow(支持GPU)

    TensorFlow 官方文档中文版 http://www.tensorfly.cn/tfdoc/get_started/introduction.html https://zhyack.github ...

  6. Android开发-eclipse+phonegap(Cordova)环境搭建

    搭建步骤: 一.安装java [官网下载].eclipse+ADT+Android SDK [点我下载x86(android-22)] | [adt-bundle-windows-x86_64-201 ...

  7. poj3613Cow Relays

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7683   Accepted: 3017 Descri ...

  8. 洛谷P1012 拼数

    题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213 又如:n=4时,4个整数7,13,4 ...

  9. javascript基本介绍

    javascript是一种广泛用于客户端web开发的脚本语言,常采用来给html网页添加动态功能,比如响应客户的各种操作. 脚本语言是什么? (1).脚本语言往往不能独立运行,它和html/jsp/p ...

  10. cp 带着属性复制过去,

    sudo cp -ra store_bak/* store/ -r   所有文件循环都复制 -a  带着属性复制过去