知识点:双指针遍历大大减少不必要的比较和计算

方法1:Brute Force(执行时间惨不忍睹,共进行n(n-1)/2次比较)

class Solution {
public:
int maxArea(vector<int>& height) {
int res=;
for(int i=;i<height.size();i++)
for(int j=i+;j<height.size();j++)
res = max(res,min(height[i],height[j])*(j-i));
return res;
}
};

方法2:双指针法( O(n) )

参考https://segmentfault.com/a/1190000008824222

class Solution {
public:
int maxArea(vector<int>& height) {
int res=;
int l=,r=height.size()-;
while(l!=r)
{
res = max(res,min(height[r],height[l])*(r-l));
if(height[l] > height[r])
r--;
else l++;
}
return res;
}
};

11. Container With Most Water C++的更多相关文章

  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. center os

    CentOS.Ubuntu.Debian三个linux比较异同 Center OS 7 安装 $$ center os 安装mysql5.6 Linux学习之Center os网络配置 Cent Os ...

  2. 51nod 1185 || 51nod 1072 威佐夫博弈

    贴个模板:平常的跟高精度java的: int: #pragma comment(linker, "/STACK:1024000000,1024000000") #include&l ...

  3. java stackoverflowerror与outofmemoryerror区别(转)

    1.stackoverflow: 每当java程序启动一个新的线程时,java虚拟机会为他分配一个栈,java栈以帧为单位保持线程运行状态:当线程调用一个方法是,jvm压入一个新的栈帧到这个线程的栈中 ...

  4. glfw3native.h and glfw3.h

    https://www.glfw.org/docs/latest/files.html

  5. python win32api 如何用代码模拟点击网页confirm框的确定按钮

    mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument; doc.parentWindo ...

  6. DP入门基本问题

    个人对简单的dp问题的理解:找是否有重叠问题,明确递推关系,怎么推的(顺序千万不要搞错),找到状态方程,循环时注意边界条件和方程式是否严格成立. 转自:https://www.cnblogs.com/ ...

  7. Codeforces 488B - Candy Boxes

    B. Candy Boxes 题目链接:http://codeforces.com/problemset/problem/488/B time limit per test 1 second memo ...

  8. Asp.net core 学习笔记 ( DI 依赖注入 )

    比起 Angular 的依赖注入, core 的相对简单许多, 容易明白 所有 provider 都在 startup 里配置. public void ConfigureServices(IServ ...

  9. Python全栈开发-Day5-常用模块学习

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil shelve xml处理 pyyaml处理 configparser hashlib re正则 ...

  10. GRCh38基因组和注释文件探究

    ensembl/release91: cat Homo_sapiens.GRCh38.91.gtf | grep -v "#" | cut -f9 | cut -f1,3,6,8 ...