user-0 提问: leetcode的Longest Palindromic Substring根据别人的代码改为c语言，求正确答案？

回复:
user: user-1
content: 抱歉之前的回答没看清你问的问题，你的代码感觉有点长，没有仔细去看，不介意的话可以用我写的这个（特意去跑了一遍，可以通过）：  char* longestPalindrome(char* s) { 	int len = strlen(s); 	if (len < 2) return s; 	int left = 0, right = 0; 	for (int i = 0; len - 1 - i > (right - left + 1) / 2;) { 		int l = i; int r = i; 		while (r < len - 1 && s[r + 1] == s[r]) r++; //找到所有的重复字母 		i = r + 1; 		while (l > 0 && r < len - 1 && s[l - 1] == s[r + 1]) {  //以重复字母片段的左右向两边扩散 			r++; 			l--; 		} 		if (r - l > right - left) { // 筛选最长的回文 			left = l; 			right = r; 		} 	} 	s[right + 1] = '\0';  	return s + left; }

user: user-1
content: 是否正确在leetcode上跑一下不就知道了么

user: user-2
content: http://blog.csdn.net/taoyanqi8932/article/details/51821442

user: user-3
content: char *s = "aadd"; 改为 char s[] = "aadd";   d = (char *)malloc(sizeof(char) * n); 改为  d = (char *)malloc(sizeof(char) * n+1);  再试试。  代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-0
content: 楼上的说的不对。。。 我是无奈啊。。。。。。。 否则也不会这么做了。。。。。。  以下代码提到leetcode中通过！！！！  #include <stdio.h> #include <stdlib.h> #include <string.h>  char* longestPalindrome(char *s) { 	char *d = NULL; 	char *rsl = NULL; 	int *p = NULL; 	int length = 0; 	int newLen = 0; 	int i = 0; 	int j = 0; 	int C = 0; 	int R = 0; 	int im = 0; 	int maxLen = 0; 	int center = 0; 	int start = 0;  	length = strlen(s); 	newLen = 2 * length + 2; 	d = (char *)malloc(sizeof(char) * newLen); 	if(d == NULL) 	{ 		printf("d is NULL..."); 		return NULL; 	} 	for(i = 0; i < length; i++) 	{ 		d[j++] = '#'; 		d[j++] = s[i]; 	} 	d[j++] = '#'; 	d[j] = '\0';  	p = (int *)malloc(sizeof(int) * newLen); 	if(p == NULL) 	{ 		printf("The p is NULL!\n"); 		return NULL; 	}  	for(i = 1; i < newLen - 1; i++) 	{ 		im = 2 * C - i; 		if(im > 0 && i + p[im] <= R) 			p[i] = p[im]; 		else 			p[i] = 0;  		while(d[i + 1 + p[i]] == d[i - 1 - p[i]] && i - 1 - p[i] >= 0)  			p[i]++;  		if(i + p[i] > R) 		{ 			C = i; 			R = i + p[i]; 		} 	}  	free(d);  	for(i = 1; i < newLen - 1;i++) 	{ 		if(p[i] > maxLen) 		{ 			maxLen = p[i]; 			center = i; 		} 	}  	free(p);  	start = center - maxLen > 0 ? (center - maxLen) / 2 : (maxLen - center) / 2; 	rsl = (char *)malloc(sizeof(char) * maxLen); 	if(rsl == NULL) 	{ 		printf("rsl is NULL......\n"); 		return NULL; 	} 	for(i = 0; i < maxLen; i++) 	{ 		rsl[i] = s[i + start]; 	} 	rsl[maxLen] = '\0'; 	return rsl; }  int main() { 	char *s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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