Day06 双指针扫描 | 11. 盛最多水的容器
·
一、11. 盛最多水的容器
1、思路
1、两重循环枚举,找冗余
2、发现关键 – 盛多少水由短的那根决定的,短的算完了就没用了
3、双指针 – 两个指针从头尾向中间移动,每次移动短的那根
2、代码
class Solution {
public int maxArea(int[] height) {
int i = 0;
int j = height.length - 1;
int result = 0;
while (i < j) {
result = Math.max(result,Math.min(height[i], height[j]) * (j - i));
if (height[i] < height[j]) {
i++;
} else {
j--;
}
}
return result;
}
}
更多推荐



所有评论(0)