11. 盛最多水的容器

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

双指针解法。 需要理解最大面积

 1 public class Solution
 2 {
 3     //双指针解法,首先要理解
 4     public int MaxArea(int[] height)
 5     {
 6         int res = 0, size = height.Length;
 7         if (height.Length == 0) return res;
 8         int i = 0, j = size - 1;
 9         while (j > i)
10         {
11             int area = (j - i) * Math.Min(height[j], height[i]);
12             if (area >= res)
13             {
14                 res = area;
15             }
16             if (height[j] > height[i])
17             {
18                 i++;
19             }
20             else
21             {
22                 j--;
23             }
24         }
25 
26         return res;
27     }
28 }
View Code

 

posted @ 2020-03-31 18:40  Quintinz  阅读(156)  评论(0编辑  收藏  举报