每日一题1

题目:11. 盛最多水的容器

采用双指针法:
两端向中间移动,每次移动短板,计算此时的容量,和res进行比较,最后留下最大的即可。

class Solution {
    public int maxArea(int[] height) {
        int n=height.length;
        int i=0;
        int j=n-1;
        int res=0,t=0;
        while(i<j){
            if(height[i]<height[j]){
                t=height[i]*(j-i);
                i++;
            }else{
                t=height[j]*(j-i);
                j--;
            }
            if(t>res) res=t;

        }

        return res;
    }
}
posted @ 2023-02-18 16:45  ZLey  阅读(19)  评论(0编辑  收藏  举报