之前没见过的双指针法((lll¬ω¬)也可能是忘了,从本大三人嘴里说出来也是有点不好意思)
开始用的是C语言两个for循环实现的,结果超时了,也可以理解
int maxArea(int* height, int heightSize)
{
int temp = 0;
int temp1 = 0;
for (int i = 0; i < heightSize; i++)
{
for (int j = i + 1; j < heightSize; j++)
{
if (height[j] < height[i])
{
temp1 = height[j] * (j - i);
}
else
{
temp1 = height[i] * (j - i);
}
if (temp1 > temp)
temp = temp1;
}
}
return temp;
}
看了官方题解的双指针法:
int maxArea(int* height, int heightSize)
{
int l = 0;
int r = heightSize - 1;
int temp = 0;
int temp1 = 0;
int flag = 0;
while (l < r)
{
flag = 0;
if (height[l] < height[r])
{
temp1 = height[l] * (r - l);
flag = 1;
}
else
{
temp1 = height[r] * (r - l);
flag = 0;
}
if (temp < temp1)
{
temp = temp1;
}
if (flag)
{
l++;
}
else
{
r--;
}
}
return temp;
}
看了Cprimerplus之后,有点进步吧
但也还要做更多题才能看出来