“abcbc” => bcb 或 cbc = 3
“abcbe” => bcb = 3
遍历每个字符时往两边扩展,两边不一样或者到头了停止
static int longestPalindrome(string input)
{
int maxLength = -1;
for (int i = 0; i < input.Length; i++)
{
int currentLength = 1;
bool moveNext = true;
int left = i;
int right = i;
while (moveNext)
{
left–;
right++;
if (left < 0 || right > input.Length - 1)
{
moveNext = false;
}
else if (input[left] == input[right])
{
currentLength = currentLength + 2;
}
else
{
moveNext = false;
}
if (currentLength > maxLength)
{
maxLength = currentLength;
}
}
}
return maxLength;
}