题解 | #盛水最多的容器# 双指针
盛水最多的容器
https://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param height int整型一维数组
* @return int整型
*/
public int maxArea (int[] height) {
// write code here
// 两个板子的最小值*宽度
int n=height.length;
int l=0,r=n-1;
int max=0;
while(l<r){
max =Math.max(Math.min(height[l],height[r])*(r-l),max);
int x=height[l]<height[r]?l++:r--;
}
return max;
}
}
盛水的容量为两个板子的最小值*宽度,宽度一定时,两个板子最小值越大,容量越大,因此每次移动较短的板子

查看17道真题和解析