盛最多水的容器
    题目链接:https://leetcode-cn.com/problems/container-with-most-water/submissions/
    图片.png

    思路:双指针,从两端开始,每次把两个板中的短板向内移一格。因为向内的过程底长是单调递减的,而面积由短板决定,因此该搜索过程不会遗漏面积更大的情况。

    参考代码:

    1. class Solution {
    2. public:
    3. int maxArea(vector<int>& height) {
    4. if (height.size() <= 1) {
    5. return 0;
    6. }
    7. int left = 0;
    8. int right = height.size() - 1;
    9. int res = 0;
    10. while (left < right) {
    11. res = max(res, min(height[left], height[right]) * (right - left));
    12. if (height[left] < height[right]) {
    13. ++left;
    14. }
    15. else {
    16. --right;
    17. }
    18. }
    19. return res;
    20. }
    21. };