首页 > 其他 > 详细

11. 盛最多水的容器

时间:2019-10-21 21:09:50      阅读:45      评论:0      收藏:0      [点我收藏+]

https://leetcode-cn.com/problems/container-with-most-water/

i指向0位置,j指向最后一个位置,每次移动指向较小数的指针。

 1 public class Solution {
 2     // 暴力解法
 3     public int maxArea(int[] height) {
 4         int max = 0;
 5         for (int i = 0; i < height.length; ++i) {
 6             for (int j = height.length-1; j > i; --j) {
 7                 max = Math.max((j-i)*Math.min(height[i], height[j]),max);
 8             }
 9         }
10         return max;
11     }
12 
13     // 双指针
14     public int maxArea(int[] height) {
15         int max = 0;
16         int i = 0, j = height.length-1;
17         while (i < j) {
18             max = Math.max(max, (j-i)* Math.min(height[i], height[j]));
19             if (height[i] < height[j])
20                 i++;
21             else
22                 j--;
23         }
24         return max;
25     }
26 
27     public static void main(String[] args) {
28         int[] arg={4,55,55,4};
29         int i = new Solution().maxArea(arg);
30         System.out.println("i = " + i);
31     }
32 }

 

11. 盛最多水的容器

原文:https://www.cnblogs.com/yfs123456/p/11715906.html

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