其他分享
首页 > 其他分享> > 11. 盛最多水的容器

11. 盛最多水的容器

作者:互联网

11. 盛最多水的容器

双指针,移动短板,更新结果

 

class Solution {
    public int maxArea(int[] height) {
        int i = 0, j = height.length - 1, res = 0;
        while(i < j) {
            res = height[i] < height[j] ?
                //(j - i)需要写到height[i++]和height[j--]前面,因为写在后面值变了
                Math.max(res, (j - i) * height[i++]):
                Math.max(res, (j - i) * height[j--]);
        }
        return res;
    }
}

 

标签:11,容器,int,res,height,max,最多水,Math
来源: https://www.cnblogs.com/deerlet/p/15502365.html