您的位置:首页 > 大数据 > 人工智能

LeetCode 11---Container With Most Water

2016-12-23 12:51 323 查看
题目链接:

LeetCode 11—Container With Most Water

本题是一个很容易想到的贪心思路,采用两个指针分别指向数组头、尾,每次移动短的那一边即可覆盖全部可能情况。

其实现代码如下:

public class Problem11 {

public static void main(String[] args) {
// TODO Auto-generated method stub

}
public static int maxArea(int[] height) {
int i=0,j=height.length-1;
int max=Math.min(height[i], height[j])*(height.length-1);
while(i!=j){
if(height[i]<=height[j]){
i++;
if(height[i]<=height[i-1])
continue;
}
else{
j--;
if(height[j]<=height[j+1])
continue;
}
max=Math.max(max, (j-i)*Math.min(height[i], height[j]));
}

return max;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 算法