首页 > 其他 > 详细

11. 盛最多水的容器

时间:2021-04-30 16:40:03      阅读:15      评论:0      收藏:0      [点我收藏+]
 1 package leetcode;
 2 
 3 public class demo_11 {
 4     public int maxArea(int[] height) {
 5         int left=0;
 6         int right=height.length-1;
 7         //保存当前最大的面积
 8         int max=height[right]<height[left] ? height[right]*(right-left):height[left]*(right-left);
 9         int area;
10         while(left<right) {
11             //左指针的高度小于右指针的高度,那么当前就是以左指针高度为边的面积最大
12             if(height[left]<height[right]) {
13                 area=height[left]*(right-left);
14                 left=left+1;
15             }
16             //右指针的高度小于右指针的高度,那么当前就是以右指针高度为边的面积最大
17             else {
18                 area=height[right]*(right-left);
19                 right=right-1;
20             }
21             if(max<area) {
22                 max=area;
23             }
24         }
25         System.out.println(max);
26         return max;
27     }
28     public static void main(String[] args) {
29         // TODO Auto-generated method stub
30         demo_11 d11=new demo_11();
31         int[] height= {1,8,6,2,5,4,8,3,7};
32         d11.maxArea(height);
33     }
34 
35 }

 

11. 盛最多水的容器

原文:https://www.cnblogs.com/Yshun/p/14722281.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!