class Solution {
public:
int maxArea(vector<int>& height) { int len=height.size();//错过,少了int
if (len<)
return ;
if (len==)
return min(height[],height[]); int i=,j=len-;
int max_vol=,cur_vol=;
while (i!=j)
{
cur_vol=(min(height[i],height[j]))*(j-i);//错过,因为j-i不用+1,这里该举例具体分析一下的
if(height[i]<=height[j])
i++;
else
j--;
if(cur_vol>max_vol)
max_vol=cur_vol;
} return max_vol;//错过,拼写错误 }
};

分析:

一开始想到双循环遍历,但是时间复杂度为O(n^2),后来想到双指针法,在距离尽可能大的情况,由于受边最小值影响,使值小指针往中间靠,这样时间复杂度O(n),也没有增加空间复杂度(都是O(1))。

leecode第十一题(盛最多水的容器)的更多相关文章

  1. leetcode 刷题(数组篇)11题 盛最多水的容器(双指针)

    题目描述 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) .找出其 ...

  2. LeetCode:盛最多水的容器【11】

    LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为  ...

  3. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

  4. 力扣Leetcode 11. 盛最多水的容器

    盛最多水的容器 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...

  5. LeetCode---11. 盛最多水的容器(Java)

    11. 盛最多水的容器 题目地址:https://leetcode-cn.com/problems/container-with-most-water/ 给你 n 个非负整数 a1,a2,...,an ...

  6. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  7. Leetcode题库——11.盛最多水的容器

    @author: ZZQ @software: PyCharm @file: maxArea.py @time: 2018/10/11 21:47 说明:给定 n 个非负整数 a1,a2,...,an ...

  8. LeetCode随缘刷题之盛最多水的容器

    package leetcode.day_01_30; /** * 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点(i,ai) .在坐标内画 n 条垂直线,垂直线 i的两个端 ...

  9. leetcode刷题11. 盛最多水的容器

    做题连接https://leetcode-cn.com/problems/container-with-most-water/submissions/ 本题分为两种方法: 暴力法: int maxAr ...

随机推荐

  1. 获取其他线程的数据用 queue, 多进程Q

    获取其他线程的数据用 queue, 多进程Q

  2. scrapy 日志处理

    Scrapy生成的调试信息非常有用,但是通常太啰嗦,你可以在Scrapy项目中的setting.py中设置日志显示等级: LOG_LEVEL = 'ERROR' 日志级别 Scrapy日志有五种等级, ...

  3. Prometheus监控学习笔记之PromQL 内置函数

    概述 Prometheus 提供了其它大量的内置函数,可以对时序数据进行丰富的处理.某些函数有默认的参数,例如:year(v=vector(time()) instant-vector).其中参数 v ...

  4. Prometheus监控学习笔记之Prometheus的架构及持久化

    0x00 Prometheus是什么 Prometheus是一个开源的系统监控和报警工具,特点是 多维数据模型(时序列数据由metric名和一组key/value组成) 在多维度上灵活的查询语言(Pr ...

  5. 微信企业号OAuth2.0验证接口来获取成员的身份信息

    <?php $appid = "请输入您企业的appid"; $secret = "请输入您企业的secreat"; if (!isset($_GET[' ...

  6. tcpdump 抓包工具使用

    1. 常用命令 监听p4p1网卡上来自 192.168.162.14 的包 tcpdump -i p4p1 src host 192.168.162.14 tcpdump -i p4p1 dst po ...

  7. 【题解】Luogu P2147 [SDOI2008]洞穴勘测

    原题传送门 这题用Link-Cut-Tree解决,Link-Cut-Tree详解 我不太会踩爆Link-Cut-Tree的并查集做法qaq 我们用Link-Cut-Tree维护连通性(十分无脑) Co ...

  8. mysql "The user specified as a definer ('root'@'%') does not exist" 问题

    在重配mysql的时候碰到, 解决办法: 重新授权 grant all privileges on *.* to root@"%" identified by ".&qu ...

  9. php 带省略号的分页

    原文链接:https://blog.csdn.net/u011060253/article/details/25308455 $curpage = isset($_GET[; $page = new ...

  10. Python3基础 dict setdefault 根据键查找值,找不到键会添加

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...