leetcode container with most water
来源:互联网 发布:珠江钢琴天猫淘宝 编辑:程序博客网 时间:2024/06/08 03:52
#include<iostream>
#include<vector>
#define min(a,b) (a>b? b:a)
#define max(a,b) (a>b? a:b)
using namespace std;
class Solution {
public:
int maxArea(vector<int>& height) {
int i=0; int j=height.size()-1;
int res=(j-i)*min(height[i],height[j]);
while(i<j){
if(height[i]<height[j]){
i++;
}
else
j--;
res=max(res,(j-i)*min(height[i],height[j]));
}
return res;
}
};
看的人家的算法简单的理解了一下
假设取I,J两边界为初始值,那么在选择下个I或者J的时候先去height[I]和height[j]中较小的那个进行改变(假设height[I]<height[j]),因为我的目的是根据找到比较小值i进行改变,这样才能使容积有可能变得更大,如果选择较大值进行改变,改变j,那是不可能使容器更大的,讨论一下就可以知道了,所以就改变I呗,我的目的是是新的height[I]增大,找到下个height[新I]大于height[I]就好了 然后在整理比大小 看谁更大
#include<vector>
#define min(a,b) (a>b? b:a)
#define max(a,b) (a>b? a:b)
using namespace std;
class Solution {
public:
int maxArea(vector<int>& height) {
int i=0; int j=height.size()-1;
int res=(j-i)*min(height[i],height[j]);
while(i<j){
if(height[i]<height[j]){
i++;
}
else
j--;
res=max(res,(j-i)*min(height[i],height[j]));
}
return res;
}
};
看的人家的算法简单的理解了一下
假设取I,J两边界为初始值,那么在选择下个I或者J的时候先去height[I]和height[j]中较小的那个进行改变(假设height[I]<height[j]),因为我的目的是根据找到比较小值i进行改变,这样才能使容积有可能变得更大,如果选择较大值进行改变,改变j,那是不可能使容器更大的,讨论一下就可以知道了,所以就改变I呗,我的目的是是新的height[I]增大,找到下个height[新I]大于height[I]就好了 然后在整理比大小 看谁更大
阅读全文
0 0
- LeetCode: Container With Most Water
- [LeetCode]Container With Most Water
- LeetCode Container With Most Water
- LeetCode: Container With Most Water
- [Leetcode] Container With Most Water
- Leetcode: Container With Most Water
- [LeetCode] Container With Most Water
- LeetCode Container With Most Water
- [Leetcode] Container With Most Water
- [LeetCode]Container With Most Water
- LeetCode-Container With Most Water
- Container With Most Water leetcode
- LeetCode - Container With Most Water
- LeetCode: Container With Most Water
- Leetcode: Container With Most Water
- 【LeetCode】Container With Most Water
- Leetcode: Container With Most Water
- LeetCode:Container With Most Water
- mews/captcha 使用实例
- resource资源加载
- inux下查找某个文件位置的方法
- Git——入门篇
- SQL操作返回
- leetcode container with most water
- 表单序列化
- poj 1426 Find The Multiple
- 自定义标签,方便你复习
- 场景Scene
- AirBook GTI电脑这么厉害,竟然有数据恢复的功能?
- div下js失效的问题
- VC++:创建,调用Win32动态链接库
- timeline,一个简单精美的自定义时间轴