Container With Most Water - LeetCode
题目链接
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的更多相关文章
- Container With Most Water -- LeetCode 11
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Container With Most Water——LeetCode
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Container With Most Water leetcode java
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- 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 ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ Container With Most Water 容器的最大装水量
题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...
随机推荐
- NO---20 文件上传
文件上传是我们会经常用到的一个业务,其实在h5中新增了FormData的对象,用它来提交表单,并且可以提交二进制文件,所以今天就写写文件上传,希望可以对大家有帮助 FormData 上传文件实例 首先 ...
- 金融科技行业 SDL(转载)
都是一些检查项,值得借鉴,关键在于要能够落地 作者 沈发挺@美的金融科技下载打印版
- js备忘录1
新建对象 赋值和取值操作 var book={ topic:"JavaScript", fat: true }; book.topic 通过点访问 book["fat& ...
- 解决maven update project 后项目jdk变成1.5
http://blog.csdn.net/jay_1989/article/details/52687934
- window 窗口编辑
package com.chuangkohua; import java.awt.FileDialog; import java.awt.FlowLayout; import java.awt.Fra ...
- lintcode-517-丑数
517-丑数 写一个程序来检测一个整数是不是丑数. 丑数的定义是,只包含质因子 2, 3, 5 的正整数.比如 6, 8 就是丑数,但是 14 不是丑数以为他包含了质因子 7. 注意事项 可以认为 1 ...
- apache 2.4目录权限
apache 2.4 好象不再支持以下指令...Order allow,denyAllow from all 用上面的指令访问页面时显示错误:client denied by server confi ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- 关于jsonp知识的理解
jsonp 之前知道是用来解决ajax跨域的问题,但是其本质的原理,还是不清楚. 所以看了一下. js的script 的src里面的连接是可以跨域的,所以可以通过她来实现跨域资源获取. 但是也需要后端 ...
- #Leetcode# 707. Design Linked List
https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...