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. spring 整合 redis 单机版

    增加spring配置文件: application-jedis.xml <?xml version="1.0" encoding="UTF-8"?> ...

  2. each jquery

    <div class="first"> <span>投保人数:</span> <input type="text" i ...

  3. JSP动态合并单元格

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <table ...

  4. cas 服务端、客服端详细配置

    一.准备 1.下载官方源码 CAS-Server下载地址:https://www.apereo.org/projects/cas/download-cas CAS-Client下载地址:http:// ...

  5. Windows、Linux及Mac查看端口和杀死进程

    本文介绍如何在Windows.Linux及Mac下查看端口和杀死进程. Windows下查看端口和杀死进程 查看占用端口号的进程号:netstat –ano | findstr "指定端口号 ...

  6. 【vijos】P1659 河蟹王国

    [算法]线段树 [题解]区间加上同一个数+区间查询最大值.注意和谐值可以是负数,初始化ans为负无穷大. #include<cstdio> #include<algorithm> ...

  7. 集合类HashMap,HashTable,ConcurrentHashMap区别?

    1.HashMap 简单来说,HashMap由数组+链表组成的,数组是HashMap的主体,链表则是主要为了解决哈希冲突而存在的,如果定位到的数组位置不含链表(当前entry的next指向null), ...

  8. Java多线程学习(二)synchronized关键字(1)

    转载请备注地址: https://blog.csdn.net/qq_34337272/article/details/79655194 Java多线程学习(二)将分为两篇文章介绍synchronize ...

  9. pcap的安装

    pcap,即 packet capture library 抓包库,这个抓包库给抓包系统提供了一个高层次的接口.所有网络上的数据包,甚至是那些发送给其他主机的,通过这种机制,都是可以捕获的.它也支持把 ...

  10. linux 3389连接工具Rdesktop

    简单使用 工作机换成战斗机了,改用ubuntu,原来的windows7上东西笔记多,还不想重装.用rdesktop来远程连接windows: sudo apt-get install rdesktop ...