11. Container With Most Water

题目链接:https://leetcode.com/problems/container-with-most-water/

 

解题思路:

目的是求出与x轴形成的面积,首先找两个数中最小的那个柱子,然后柱子高度×(j-i)

如果i的高度小于j的高度,i++

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

 

posted @ 2019-05-07 18:18  王爷爱吃秋刀鱼  阅读(101)  评论(0编辑  收藏  举报