提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
题目列表
【第1题】 盛最多水的容器
LeetCode地址:https://leetcode.cn/problems/container-with-most-water/
1.题目描述
2.解题思路
使用双指正,分别指数组的头元素和尾元素。依次计算头尾指针的最大容量,之后移动头尾指针中小的元素,循环计算。最终返回最大值。
3.代码详解
class Solution {
public int maxArea(int[] height) {
int max = 0;
int i = 0;
int j = height.length - 1;
while(i < j){
max = Math.max(max,Math.min(height[i],height[j]) * (j - i));
if(height[i] < height[j]){
i++;
}else{
j--;
}
}
return max;
}
}