leetcode_11 ContainerWithMostWater(Java)

在这里插入图片描述

我的做法:使用一个数组装载可能的结果,再排序后输出最大值。

class Solution {
            public int maxArea(int[] height) {
                          int l = 0;
                          int r = height.length - 1;
                          int[] sum = new int[height.length];
                          int index = 0;
                          while (l<r){
                               if(height[r] >= height[l]){
                                  sum[index] = height[l]*(r-l);
                                  index ++;
                                  l++;
                               }else if(height[r] < height[l]){
                                    sum[index] = height[r]*(r-l);
                                    index ++;
                                    r--;
                              }
                            }
         Arrays.sort(sum);
        return sum[sum.length-1];
        }
        }
     
 

猜你喜欢

转载自blog.csdn.net/weixin_42112064/article/details/85382632