We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接:https://leetcode-cn.com/problems/container-with-most-water/
解题思路:
可以参考官方题解和双指针法正确性证明。
/** * @param {number[]} height * @return {number} */ var maxArea = function (height) { let result = 0; // 缓存结果 // 使用for循环遍历,可以省去两行声明指针代码,当i与j相遇时,退出循环 for (let i = 0, j = height.length - 1; i < j; ) { // 查找较矮的高度,将其用于计算面积,之后将取值过的指针向内移动,继续遍历。 const minHeight = height[i] < height[j] ? height[i++] : height[j--]; // 计算当前面积,由于计算高度之后,指针已经移动过一次,此处宽度要加1 const area = minHeight * (j - i + 1); // 对比当前面积和缓存的面积,保存最大值 result = Math.max(area, result); } // 退出循环时,缓存的值就是最大值。 return result; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:https://leetcode-cn.com/problems/container-with-most-water/
解题思路:
可以参考官方题解和双指针法正确性证明。
The text was updated successfully, but these errors were encountered: