题目链接

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. 戴尔win10重新安装win7系统

    戴尔v5468电脑win10重装回win7系统 首先是公司需要用到ie8来执行公司的项目维护,都是很早之前的项目了,因为是对接政府相关的业务,不怎么有把握对项目进行稳定更新,所以我就为这个ie8操碎了 ...

  2. Windows搭建python开发环境

    python你不去认识它,可能没什么,一旦你认识了它,你就会爱上它 基本概念Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum ...

  3. Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValuesByConditionsNoPage

    报Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValu ...

  4. Daily Scrum 11.18

    今日完成任务: 1.在提问问题的时候为问题创建索引 2.解决了修改个人资料后刷新没有更新的问题 3.初步加入了采纳功能(没完善UI设计) 遇到困难:创建索引之后,跳转到主页,需要重新登录,找了半天不知 ...

  5. 团队计划backlog---DayTwo

    任务索引卡(Two): 1.  季方:实现界面跳转,数据库相关数据的显示 的测试: 2.  司宇航:添加部分团队博客,并测试: 3.  王金萱.马佳慧:学习爬虫的相关内容,为将来统计博客部分做准备: ...

  6. VirtualBox安装增强功能

    一.安装依赖包 #yum install kernel-headers #yum install kernel-devel #yum install gcc* #yum install make 二. ...

  7. 关于httpServlet.service()步骤

    关于httpServlet.service()步骤 关于()方法 1.HTTP Servlet 使用一个 HTML 表格来发送和接收数据.要创建一个 HTTP Servlet,就需要扩展 HttpSe ...

  8. 项目Beta冲刺(团队)第三天

    1.昨天的困难 记住密码打勾之后点击登录记住密码这四个字会变成省略号 点赞点击以后本应该呈现的爱心形状变成了方块 2.今天解决的进度 成员 进度 陈家权 私信模块探索ing,回复详情界面设计 赖晓连 ...

  9. BNUOJ 52317 As Easy As Possible 树上倍增/主席树

    题目链接: https://acm.bnu.edu.cn/v3/problem_show.php?pid=52317 As Easy As Possible Case Time Limit: 1000 ...

  10. Aspose 插件

    百度:Aspose Aspose.Cells.dll Aspose.Slides.dll Aspose.Words.dll