263 字
1 分钟
LeetCode-11. 盛最多水的容器
双指针
给定一个长度为
n的整数数组height。有n条垂线,第i条线的两个端点是(i, 0)和(i, height[i])。找出其中的两条线,使得它们与
x轴共同构成的容器可以容纳最多的水。返回容器可以储存的最大水量。
**说明:**你不能倾斜容器。
示例 1:
输入:[1,8,6,2,5,4,8,3,7]输出:49解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。示例 2:
输入:height = [1,1]输出:1
1.自己的方法(双指针)
比较简单的中等题
思路:在减小宽度时时,永远只移动较短的那块木板
func maxArea(height []int) int { left := 0 right := len(height) - 1 h := 0 max := 0 for left < right { if height[left] < height[right] { h = height[left] }else { h = height[right] }
if max < h * (right - left) { max = h * (right - left) }
if height[left] < height[right] { left++ }else{ right-- } } return max} LeetCode-11. 盛最多水的容器
https://sheep44044.github.io/posts/算法/双指针/leetcode-11-盛最多水的容器/
