题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
#include <stdio.h>
#include <string.h>
int main() {
char str1[351], str2[351];
scanf("%s", str1);
int max = 0;
for(int i = 0; i<strlen(str1)-1; i++) //从左至右遍历,先判断第一次遇到的地方往回判断是否为回文(标准即左进到位置=右进到的位置),否则往后判断直至结束;二次左进1位再遍历,保证遍历全面。
{
for(int j = i+1; j<strlen(str1); j++)
{
int m = i, n = j;
while(m<n)
{
if(str1[n] != str1[m])
break;
else
{
m++;
n--;
}
}
if(m>=n)
{
max = (max>(j-i+1)) ? max : (j-i+1);
}
}
}
printf("%d", max);
return 0;
}
查看9道真题和解析
