Leetcode:11盛最多水的容器
题目描述
解题思路
双指针,分别指向数组头和尾,进行遍历,计算初始的面积,看左右哪个高度低,就遍历那一边,左边小就i++,右边小就i–,直到i=j,每次循环出来判断当前面积是否最大,遍历结束返回count。
代码
int maxArea(int* height, int heightSize) {
int i=0;
int j=heightSize-1;
int count=0;
int s=0;
while(i<j){
if(height[i]<height[j]){
s=(j-i)*height[i];
i++;
}else{
s=(j-i)*height[j];
j--;
}
if(count<s){
count=s;
}
}
return count;
}