您的位置:首页 > 大数据 > 人工智能

leetcode-011:Container With Most Water

2013-09-02 22:15 274 查看
class Solution {
public:
int maxArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int result=0;
if(height.size()<=1) return result;

int i=0,j=height.size()-1;
while(i<j)
{
result=max(result,(j-i)*min(height[i],height[j]));
if(height[i]<height[j])
i++;
else
j--;
}
return result;
}
};


初始思路,任意的i,j 计算

优化思路,如果height[i]<height[j],则i-1和j 一定比之前的最大值小,没有必要进行计算;同理,右边。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: