Thursday, November 19, 2015

[leetcode] Container With most water

拼命往难处想 其实最简单的方法 双指针就可以了
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class Solution {
    public int maxArea(int[] height) {
        if (height == null||height.length==0) return 0;
        int left = 0;
        int right = height.length-1;
        int curMax = 0;
        while (left < right){
            curMax = Math.max(Math.min(height[left], height[right])*(right-left), curMax);
            if (height[left] > height[right]) right--;
            else left++;
        }
        return curMax;
    }
}

No comments:

Post a Comment