题目链接:https://leetcode.cn/problems/container-with-most-water/description/?envType=study-plan-v2&envId=top-100-liked
双指针
相向双指针,判断哪边指针指向的值小一些,就往中间移动
class Solution {
public int maxArea(int[] height) {
int n = height.length;
//存储答案
int res = 0;
//相向双指针
for(int i = 0, j = n - 1; i < j;){
//当前的面积
int cnt = (j - i) * Math.min(height[i], height[j]);
//求最大的面积
res = Math.max(res, cnt);
//如果左指针指向的值小,左边就往中间移动,否则就右指针移动
if(height[i] < height[j]){
i++;
}else{
j--;
}
}
return res;
}
}
还剩95题!