11. Container With Most Water

一、题目

  1、审题:

    

   2、分析:

     给出纵坐标的数组,求两个坐标所能组成的最大面积。

二、解答

  1、分析:

    a、记数组下标 low=0 和 high=数组长-1;

    b、low 和 high 相向而行,求得所组成最大面积;

      其中判断条件为,若 low 所在纵坐标 比 high所在纵坐标大,则 high--,否则 low++;

      记录每一次坐标所组成的面积大小;

      若 low >= high, 则跳出循环;

class Solution {
    public int maxArea(int[] height) {

        int maxArea = 0, tempArea = 0;
        int low = 0, high = height.length - 1;

        while(low < high) {

            tempArea = (high - low) * Math.min(height[low], height[high]);
            if(tempArea > maxArea)
                maxArea = tempArea;

            if(height[low] < height[high])
                low++;
            else
                high--;

        }

        return maxArea;
    }
}
原文地址:https://www.cnblogs.com/skillking/p/9403791.html