题目地址:https://leetcode-cn.com/problems/container-with-most-water/
解题思路: 很明显这类题目不能用暴力,很容易超时;可以采用贪心的想法:最大的体积肯定要么很宽,要么很高。所以从两边开始计算,然后较小的边界舍去,边界向左或者向右移动一个,再次计算。
class Solution { public: int maxArea(vector<int>& height) { int returnS = 0; int left = 0; int right = height.size() - 1; int tmp; while (left < right) { tmp = min(height[left], height[right])* (right - left); returnS = max(tmp, returnS); if (height[left] < height[right]) left++; else right--; } return returnS; } };