011盛最多水的容器

写在前面,参考的是力扣官网的图解

一、java算法

/*
 * @lc app=leetcode.cn id=11 lang=java
 *
 * [11] 盛最多水的容器
 */

// @lc code=start
class Solution {
    public int maxArea(int[] height) {

        //设置两个变量,两头i,j
        int i = 0, j = height.length - 1, res = 0;
        while(i < j){

            //选择短边向内收缩
            res = height[i] < height[j] ? 

                //三元数组,递归循环
                Math.max(res, (j - i) * height[i++]): 
                Math.max(res, (j - i) * height[j--]); 
        }
        return res;
    }
}
// @lc code=end

二、图解

仔细看完其实很简单的,他就是每次向内扩的时候,选择较小的那个往内扩

2.1

2.2

posted @ 2020-08-09 19:25  阿狸狸爱吃饭  阅读(57)  评论(0编辑  收藏  举报