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.

用两个指针从数组的左边和右边开始,向中间搜索。依据的理由有两点:
1、因为一开始底边长就已经是最大,两个指针向中间移动的时候,底边长只会变短,因此如果此时面积要变大的话,只能是两条高中的最短者比移动前的最短高更高,   
2、因此,每次选择移动左指针还是右指针,优先选择当前高度最短的那个,以期寻找到更高的边。如果此时选择移动当前高度最高的那个,就有可能跳过了最优解。

直观的解释是:容积即面积,它受长和高的影响,当长度减小时候,高必须增长才有可能提升面积,所以我们从长度最长时开始递减,然后寻找更高的线来更新候补;

没怎么用过Python写过程序,所以决定用Python来写写,结果错误百出。

出错的地方如下:

1,python在for循环,while,if等语句中不用括号,应该用缩进和:

2,和c++一样python需要特比的注意中文输入,这个程序就是有个中文的:,害得我一直不知道错在哪.

class Solution:
# @param {integer[]} height
# @return {integer}
def maxArea(self, height):
Maxlenth=len(height)
if == Maxlenth:
return
StartPos=
MaxLeft=height[StartPos]
EndPos=Maxlenth-
MaxRight=height[EndPos]
MaxCont=
while StartPos<EndPos:
if height[StartPos]>=height[EndPos]:
T=height[EndPos]*(EndPos-StartPos)
EndPos-=
if height[EndPos]>MaxRight:
MaxRight=EndPos
else:
T=height[StartPos]*(EndPos-StartPos)
StartPos+=
if height[StartPos]>MaxLeft:
MaxLeft=StartPos
if T>MaxCont:
MaxCont=T
return MaxCont

Container With Most Water——双指针的更多相关文章

  1. Container With Most Water - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Container With Most Water - LeetCode 注意点 没什么好注意的... 解法 解法一:暴力求解,假设任意两个端点会是最佳答 ...

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

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

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

  4. [LintCode] Container With Most Water 装最多水的容器

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

  5. 67. Container With Most Water

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

  6. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  7. No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  8. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

  9. 关于Container With Most Water的求解

    Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...

随机推荐

  1. JavaScript关键字return的用法

    return 语句从当前函数退出,并从那个函数返回一个值. 语法: 1 return[()[expression][]]; 可选项 expression 参数是要从函数返回的值.如果省略,则该函数不返 ...

  2. Codeforces Round #343 (Div. 2) B

    B. Far Relative’s Problem time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  3. JS 中类型鉴别

    JS中的基本类型有:数字(Number(NaN,Infinity)),字符串(String),Undefined,Null,Boolean 引用类型有:数组(Array),对象(Object),函数( ...

  4. stout代码分支之十二:巧妙的EXIT

    在c++中,为了便于定位问题,进程异常退出时,需要获取返回码和错误信息.stout中将这种功能巧妙的封装成EXIT类. #define EXIT(status) __Exit(status).stre ...

  5. Beautiful Soup的一些中文资料

    如果你着急用的话,可以看下这个简略版的,非常简单: 转自  人世间http://rsj217.diandian.com/post/2012-11-01/40041235132 当然,强烈推荐你看一下的 ...

  6. jquery字符串序列化方法总结

    在jquery中字符串序列化方法包括有param() .serialize() .serializeArray(),在这里对其常用做法进行总结. $.param()方法这是serialize()方法的 ...

  7. 关于scala 集合 List Map Set

    1,数组 2,List,ListBuffer 3, Map , mutable.Map

  8. niceScroll 简单使用 及 插件API

    官方网址[https://nicescroll.areaaperta.com/]  注:效果见官网右侧滚动条 jquery.nicescroll文件下载地址 引入核心文件,插件需要引入1.5.X以上版 ...

  9. Django rest framework + Vue简单示例

    构建vue项目参考这篇文章https://segmentfault.com/a/1190000008049815 一.创建Vue项目 修改源:npm config set registry https ...

  10. monkey测试===什么是monkey测试(系列一)转

    本文转自:http://www.cnblogs.com/liu-ke/p/4353926.html Monkey工具使用 一. 什么是Monkey Monkey是Android中的一个命令行工具,可以 ...