11. 盛最多水的容器

class Solution {
    public int maxArea(int[] height) {
        
        int count=0;
        int su=0;
        for (int i=0;i<height.length;i++){
            for(int j=i+1;j<height.length;j++){
                if (height[i]<=height[j]){
                    count=(j-i)*height[i];
                }else {
                    count=(j-i)*height[j];
                }
                if(count>=su){
                    su=count;
                }
            }
        }
        return su;
    }
}

607ms

大神的做法:

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

6ms

都是遍历,但是效果很不同

posted @ 2019-07-06 13:03  leolaosao  阅读(134)  评论(0编辑  收藏  举报