llllmz

导航

11. 盛最多水的容器c

int maxArea(int* height, int heightSize) {
    int max=0;
    int head=0,tail=heightSize-1;
    while(head<tail){
        int sum;
        if(height[head]<height[tail]){
            sum=height[head]*(tail-head);
            head++;
        }else if(height[head]>height[tail]){
            sum=height[tail]*(tail-head);
            tail--;
        }else{
            sum=height[head]*(tail-head);
            head++;
            tail--;
        }
        if(max< sum)  max=sum;       
    }
    return max;
}

双指针

posted on 2024-03-12 15:11  神奇的萝卜丝  阅读(17)  评论(0)    收藏  举报