Problem: 11. 盛最多水的容器
解析
双指针做法
left,right指针分别位于两侧,移动指针只能移动较小的指针,否则不会得到更大的面积。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public: int maxArea(vector<int>& height) { int res = 0; int left = 0, right = height.size() - 1; while (left < right) { res = max(res, min(height[left], height[right]) * (right - left)); if (height[left] <= height[right]) { ++left; } else { --right; } } return res; } };
|