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
都是遍历,但是效果很不同