您的位置:首页 > 其它

leetcode 11.盛最多水的容器

2018-03-25 18:31 405 查看

11. 盛最多水的容器

双指针法,挺常用的,学习一下。

class MaxArea {
public:
int maxArea(vector<int>& height) {
if (height.size() < 2)
{
return 0;
}
int nLeft = 0;
int nRight = height.size() - 1;
int nMaxArea = 0;
while (nLeft < nRight)
{
int nLeftVal = height[nLeft];
int nRightVal = height[nRight];
int nVal = nLeftVal < nRightVal ? nLeftVal:nRightVal;
if (nMaxArea < nVal * (nRight - nLeft))
{
nMaxArea = nVal * (nRight - nLeft);
}
if (nLeftVal < nRightVal)
{
nLeft++;
}
else
{
nRight--;
}
}
return nMaxArea;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: