Leetcode题解之Container With Most Water

1、题目描述

2、题目分析

 首先,这个题可以使用暴力解法,时间复杂度是O(n^2),这个显然是最容易的做法,但是效率不够高,题目提供了一种解法,使用两个指针,一个从头向尾部,另外一个从尾部向头部,每一步寻找最大的面积,然后较小的一边向前移动。

3、代码实现

 1 int maxArea(vector<int>& height) {
 2         
 3         int max_area = 0;
 4         for( vector<int>::iterator pb = height.begin() , pe = height.end() - 1; pb  < pe ; )
 5         {
 6             max_area = max( max_area ,min(*pb , *pe)*(int)(pe -pb));
 7             if( *pb < *pe)
 8                 pb++;
 9             else
10                 pe--;
11         }
12         return max_area;
13         
14     }

猜你喜欢

转载自www.cnblogs.com/wangxiaoyong/p/9286067.html