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, ...
随机推荐
- numpy技巧
矩阵旋转: mat_array = np.array([[1,2],[4,3]]) rotate_90 = np.zeros((2,2), np.uint8) rotate_180 = np.zero ...
- Controller层@PathVariable使用
@PathVariable 映射 URL 绑定的占位符 带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义通过 @Pa ...
- [Codeforces-911B] - Two Cakes
B. Two Cakestime limit per test 1 secondmemory limit per test 256 megabytesinput standard inputoutpu ...
- 使用python实现解析二元一次方程
二元一次函数的实现 import cmathimport mathimport sys 这里导入cmath包是在后面用来处理复数的情况导入math使用来处理 平方 根号等的运算而导入sys的意义是为了 ...
- 利用jsencrypt 做非对称加密
1.生成 private key openssl genrsa -out rsa_1024_priv.pem 1024 2.生成public key openssl rsa -pubout -in r ...
- socket 编程 : shutdown vs close
TCP/IP 四次挥手 首先作者先描述一下TCP/IP 协议中四次挥手的过程,如果对此已经熟悉的读者可以跳过本节. 四次挥手 这是一个很经典的示例图,众所周知tcp socket 在一个生命周期中有很 ...
- 笨办法学Python - 习题3: Numbers and Math
目录 习题 3: 数字和数学计算 算术运算符 加分习题: 我的答案: 总结: 扩展: Python比较运算符 Python赋值运算符 Python位运算符 Python逻辑运算符 Python成员运算 ...
- java 实验1
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1352 姓名:杨光 学号:20135233 成绩: 指导教师:娄嘉鹏 实验 ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Ninth Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Ninth Week ...
- static 继承
静态方法大家应该都比较熟悉,在这里主要谈一下静态方法在继承时的一些注意事项. 1.父类方法如果是静态方法,子类不能覆盖为非静态方法: 2.父类方法如果是非静态方法,子类不能覆盖为静态方法: 3.父类静 ...