33,311
社区成员
发帖
与我相关
我的任务
分享
char* longestPalindrome(char* s) {
char ans={""},tmp={""};
int tmp_i,tmp_j;
for(int i = 0; i<strlen(s); i++)
for(int j = i+1; j<strlen(s); j++)
{
if(s[i] == s[j]){
//保存i,j的值
tmp_i = i; tmp_j = j;
//检查i,j之间是否对称
while(s[++i] == s[--j] && j>i);
//对称的情况
if(i >= j){
strncpy(tmp,s+tmp_i,tmp_j-tmp_i);
ans = strlen(tmp)>strlen(ans)?tmp:ans;
}
}
}
return ans;
}