解题思路:固定从头开始的第一个字符串,一个从尾开始,分别遍历每一个字符,看是否与固定的字符组成最长回文字符串。
超时超时啊,有点想不通啊!
char * longestPalindrome(char * s)
{int i,j;int start,len;int length = strlen(s);if(length < 2)return s;len = 0;for(i = 0; i < length; i++){for(j = length; j > i; j--){int m = i;int n = j;while((s[m++] == s[n--]) && (m < n));if((s[m-1] == s[n+1]) && m-n <= 2 ){if(len < j - i + 1){len = j - i + 1;start = i;if((len > length / 2) && (i > length / 2 + 1)){i = length;break;}}}}}if(len == 0){s[1] = '\0';return s;}s[start+len] = '\0';return s+start;/*char* c = (char*)malloc(sizeof(char)*(len+2));if(len == 0){c[0] = s[0];c[1] = '\0';return c;}strncpy(c, &s[start], len);c[len] = '\0';int k = 0;while(len-- > 0){c[k] = s[start+k];k++;}c[k] = '\0';return c;*/
}