思路
1.取左右两侧数字
2.依次向内取最大的
3.右侧下标等于左侧时停止
代码
int maxArea(int* height, int heightSize) {
int max, l, r, f, t;
l = 0;
r = heightSize - 1;
if (height[l] > height[r])
{
max = r * height[r];
f = 0;//右小
}
else
{
max = r * height[l];
f = 1;//左小
}
int i = 1, j;
j = r - 1;
while(i<j)
{
if(f)
{
if(height[i] >= height[r])
{
t = (r - i) * height[r];
if(t > max)
{
max = t;
l = i;
f = 0;
}
}
else if(height[i] > height[l])
{
t = (r - i) * height[i];
if(t > max)
{
max = t;
l = i;
f = 1;
}
}
if(height[j] >= height[r])
{
t = (r - j) * height[r];
if(t > max)
{
max = t;
l = j;
f = 0;
}
}
else if(height[j] > height[l])
{
t = (r - j) * height[j];
if(t > max)
{
max = t;
l = j;
f = 1;
}
}
}
else
{
if(height[i] >= height[l])
{
t = (i - l) * height[l];
if(t > max)
{
max = t;
r = i;
f = 1;
}
}
else if(height[i] > height[r])
{
t = (i - l) * height[i];
if(t > max)
{
max = t;
r = i;
f = 0;
}
}
if(height[j] >= height[l])
{
t = (j - l) * height[l];
if(t > max)
{
max = t;
r = j;
f = 1;
}
}
else if(height[j] > height[r])
{
t = (j - l) * height[j];
if(t > max)
{
max = t;
r = j;
f = 0;
}
}
}
if (height[i] > height[j])
{
t = (j - i) * height[j];
if(t > max)
{
max = t;
l = i;
r = j;
}
f = 0;//右小
j--;
}
else
{
t = (j - i) * height[i];
if(t > max)
{
max = t;
l = i;
r = j;
}
f = 1;//左小
i++;
}
}
return max;
}