*LeetCode-Container With Most Water

来源:互联网 发布:vmware fusion 8 mac 编辑:程序博客网 时间:2024/04/29 17:27

读题就读了很久 

解法很巧妙 两个指针在两端 向中心挪动 因为只要其中一个高 它下次就不用挪 挪另一个 因为挪了没有意义 过程中keep max area

public class Solution {    public int maxArea(int[] height) {        int i = 0;        int j = height.length - 1;        int area = 0;        while ( i < j ){            area = Math.max( area, Math.min(height[i], height[j]) * (j-i) );            if ( height[i] > height[j] )                j --;            else                i ++;        }        return area;    }}



0 0