当先锋百科网

首页 1 2 3 4 5 6 7

思路:

https://leetcode-cn.com/problems/container-with-most-water/solution/sheng-zui-duo-shui-de-rong-qi-by-leetcode-solution/

 

暴力解法

class Solution {public int maxArea(int[] height) {int maxarea=0;int len=height.length;for(int i=0;i<len-1;i++){for(int j=i+1;j<len;j++){maxarea=Math.max(maxarea,Math.min(height[i],height[j])*(j-i));}}return maxarea;}
}

双指针

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