题目链接

Container With Most Water - LeetCode

注意点

  • 没什么好注意的...

解法

解法一:暴力求解,假设任意两个端点会是最佳答案,逐个比较。时间复杂度为O(n^2)

class Solution {
public:
int maxArea(vector<int>& height) {
int i,j,n = height.size(),max = 0;
for(i = 0;i < n;i++)
{
for(j = i;j < n;j++)
{
int h = height[i] < height[j] ? height[i]:height[j];
int temp = (j-i)*h;
if(temp > max)
{
max = temp;
}
}
}
return max;
}
};

解法二:维护两个指针left和right,开始时一个指向数组开头,一个指向数组结尾。每次移动对应的高度较小的那个指针,两者不断的靠近,直到相遇。原因是移动指针时会导致宽度的减小,如果移动高度小的指针,就有可能抵消宽度减小带来的对面积的影响。时间复杂度O(n)

class Solution {
public:
int maxArea(vector<int>& height) {
int left = 0,right = height.size()-1,max = 0;
while(left < right)
{
int h = height[left] < height[right] ? height[left]:height[right];
int temp = h * (right - left);
if(temp > max)
{
max = temp;
}
if(height[left] < height[right])
{
left++;
}
else
{
right--;
}
}
return max;
}
};

小结

  • 维护双指针是比较常见的解题技巧

Container With Most Water - LeetCode的更多相关文章

  1. Container With Most Water -- LeetCode 11

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

  2. Container With Most Water——LeetCode

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

  3. Container With Most Water leetcode java

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

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

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

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

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

  6. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

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

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

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

  8. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. LeetCode OJ Container With Most Water 容器的最大装水量

    题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...

随机推荐

  1. ubuntu添加国内源

    安装Ubuntu 18.04后,使用国外源太慢了,修改为国内源会快很多. 修改阿里源为Ubuntu 18.04默认的源 备份/etc/apt/sources.list#备份cp /etc/apt/so ...

  2. Oz 创建Ubuntu镜像

    参考链接: http://blog.csdn.net/gcogle/article/details/52767135http://tlinux.blog.51cto.com/7288656/17497 ...

  3. rest_framework_api规范

    目录 一.什么是RESTful 二.什么是API 三.RESTful API规范 四.基于Django实现API 五.基于Django Rest Framework框架实现 一. 什么是RESTful ...

  4. 解决maven update project 后项目jdk变成1.5

    http://blog.csdn.net/jay_1989/article/details/52687934

  5. NIO中的Buffer

    public abstract class Buffer { // Invariants: mark <= position <= limit <= capacity private ...

  6. sql经常出现死锁解决办法

    文章:sql server在高并发状态下同时执行查询与更新操作时的死锁问题

  7. SQL语句中order_by_、group_by_、having的用法区别

    order by 从英文里理解就是行的排序方式,默认的为升序. order by 后面必须列出排序的字段名,可以是多个字段名. group by 从英文里理解就是分组.必须有“聚合函数”来配合才能使用 ...

  8. 1014 C语言文法定义

    <程序>→<外部声明>|<程序><外部声明><外部声明>→<函数定义>|<声明><函数定义>→<数 ...

  9. HDU 2140 Michael Scofield's letter

    http://acm.hdu.edu.cn/showproblem.php?pid=2140 Problem Description I believe many people are the fan ...

  10. php推送

    需求: 我想做个会员站内通知的功能.不想用以前的ajax查询,听说有个推技术.以下文章介绍的不错,来自转载, ============================================= ...