前言

这题非要说贪心的话也算是吧,不过最主要的特征还是双指针。LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的。这道题倒是“短板效应”的不错体现了。

题目

题目链接

Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) 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.

思路

图片看不清楚的话可右键复制链接转到图床再看。

代码

 class Solution {
public:
int maxArea(vector<int>& height) {
int first = ;
int end = height.size() - ;
int result = INT_MIN;
while (first < end)
{
int width = end - first;
int board = height[end]<height[first]?height[end]:height[first];
int area = board*width;
result = result>area?result:area;
if (height[first] <= height[end])
first++;
else
end--;
}
return result;
}
};
 

其他

JAVA果然过得比CPP快,Python和C#依旧垫底。

[LeetCode] Container With Most Water 简要分析的更多相关文章

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

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

  2. Leetcode:Container With Most Water分析和实现

    题目大意是提供n个非负整数,a1,...,an,可以将其理解为多个边界(垂直的线段),其中ai处于x坐标i处,并且ai代表的线段高度即为ai的值.要求我们取这样的i与j,使得ai与aj以及x坐标轴围成 ...

  3. [LeetCode] Container With Most Water 装最多水的容器

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

  4. [LeetCode]Container With Most Water, 解题报告

    前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...

  5. C++LeetCode:: Container With Most Water

    本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...

  6. leetcode Container With Most Water

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

  7. [Leetcode] Container With Most Water ( C++)

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

  8. LeetCode——Container With Most Water

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

  9. LeetCode Container With Most Water (Two Pointers)

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

随机推荐

  1. cojs 西瓜 解题报告

    首先我们要知道pick公式 设二维平面内任意多边形面积为S 设多边形内部整点数为a 设多边形边界的整点数为b 则满足S=a+b/2-1 变形得a=S-b/2+1 由期望的线性性质我们把问题转化为 1. ...

  2. 《HTTP权威指南》笔记

    http://blog.csdn.net/sunorry?viewmode=contents有些笔记 MIME 类型是一种文本标记,表示一种主要的对象类型和一个特定的子类型,中间由一条斜杠来分隔:te ...

  3. [Python]读写文件方法

    http://www.cnblogs.com/lovebread/archive/2009/12/24/1631108.html [Python]读写文件方法 http://www.cnblogs.c ...

  4. asp.net中当服务器出错时显示指定的错误页面

    http://blog.csdn.net/helloxiaoyu/article/details/2943537 此篇文章描述了当异常再ASP.NET中发生时怎样使用C#.NET代码去拦截和相应异常. ...

  5. PCB走线角度选择 — PCB Layout 跳坑指南

    现在但凡打开SoC原厂的PCB Layout Guide,都会提及到高速信号的走线的拐角角度问题,都会说高速信号不要以直角走线,要以45度角走线,并且会说走圆弧会比45度拐角更好.狮屎是不是这样?PC ...

  6. python list去重的方法

    转载于:http://yxmhero1989.blog.163.com/blog/static/112157956201381443244790/ Python很简洁 我们喜欢简单有效的代码   一. ...

  7. jQuery插件开发(转)

    jQuery插件开发 - 其实很简单 [前言]jQuery已经被广泛使用,凭借其简洁的API,对DOM强大的操控性,易扩展性越来越受到web开发人员的喜爱,我在社区也发布了很多的jQuery插件,经常 ...

  8. Nmap扫描原理与用法

    Nmap扫描原理与用法 1     Nmap介绍 Nmap扫描原理与用法PDF:下载地址 Nmap是一款开源免费的网络发现(Network Discovery)和安全审计(Security Audit ...

  9. SH1B LMR62014XMFE/NOPB

    制造商National Semiconductor (TI) RoHS 输出电压20 V 输出电流1.4 A 输入电压2.7 V to 14 V 开关频率1.6 MHz 最大工作温度+ 85 C 安装 ...

  10. jboolean

    bool为C中变量类型,jboolean 为JNI中变量类型,boolean为Java中变量类型:jboolean在C语言的定义为:typedef unsigned char jboolean;uns ...