本文主要是介绍LeetCode【11】 盛水最多的容器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
分析:
1、双指针,储水为(R-L )* 二者较小高度,如题目,(9-2)* 7 = 49
2、双指针向中间靠,每次移动较矮的指针。
代码:
public int maxArea(int[] height) {int left = 0;int right = height.length - 1;int max = 0;while (left < right) {if (height[right] > height[left]) {max = Math.max(max, (right - left) * height[left]);left++;} else {max = Math.max(max, (right - left) * height[right]);right--;}}return max;}
这篇关于LeetCode【11】 盛水最多的容器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!