• 博客访问： 66409
• 博文数量： 29
• 博客积分： 0
• 博客等级： 民兵
• 技术积分： 224
• 用 户 组： 普通用户
• 注册时间： 2014-03-06 15:31

2015年（18）

2014年（11）

2015-01-22 17:33:11

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container.

1. public class Solution {
2.     public int maxArea(int[] height){
3.         int len = height.length;
4.         int result = 0;
5.         for(int i = 0; i < len; i++){
6.             for(int j = i+1; j < len; j++){
7.                 int tempresult = 0;
8.                 if(height[i] < height[j]){
9.                     tempresult = height[i] * (j-i);
10.                 }
11.                 else{
12.                     tempresult = height[j] * (j-i);
13.                 }
14.                 if(tempresult > result){
15.                     result = tempresult;
16.                 }
17.             }
18.         }
19.         return result;
20.     }
21. }

1. public class Solution {
2.     public int maxArea(int[] height) {
3.         int len = height.length;
4.         int result = 0;
5.         int i = 0;
6.         int j = len - 1;
7.         while(i < len && j > i){
8.          result = Math.max(result,(j-i)*Math.min(height[i],height[j]));
9.          if(height[i] > height[j]){
10.               j--;
11.          }
12.          else{
13.               i++;
14.          }
15.         }
16.         return result;
17.     }
18. }