栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

leetcode 11. 盛最多水的容器 [java][双指针]

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode 11. 盛最多水的容器 [java][双指针]

leetcode 11. 盛最多水的容器

    static
            //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        
        public int maxArea(int[] heightArr) {
            int leftPos = 0;
            int rightPos = heightArr.length - 1;
            int maxArea = 0;
            int maxHeight = 0;
            int leftHeight = heightArr[leftPos];
            int rightHeight = heightArr[rightPos];
            while (leftPos < rightPos) {
//                矩形高度是左右两根柱子的较小值
                int height = Math.min(leftHeight, rightHeight);
                if (height > maxHeight) {
//                    只有矩形高度增加了, 面积才有可能增加
                    maxArea = Math.max(maxArea, height * (rightPos - leftPos));
                }
//                把较小的柱子索引往中间移动
                if (leftHeight <= rightHeight) {
                    leftHeight = heightArr[++leftPos];
                } else {
                    rightHeight = heightArr[--rightPos];
                }
            }
            return maxArea;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659349.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号