string longestPalindrome(string s) {
if (s.empty()) return "";
if (s.size() == 1) return s;
int min_start = 0, max_len = 1;
for (int i = 0; i < s.size();) {
if (s.size()  i <= max_len / 2) break;
int j = i, k = i;
while (k < s.size()1 && s[k+1] == s[k]) ++k; // Skip duplicate characters.
i = k+1;
while (k < s.size()1 && j > 0 && s[k + 1] == s[j  1]) { ++k; j; } // Expand.
int new_len = k  j + 1;
if (new_len > max_len) { min_start = j; max_len = new_len; }
}
return s.substr(min_start, max_len);
}
Simple C++ solution (8ms, 13 lines)


Wonderful solution! Thanks~
I rewrite it with C and use pointers.
runtime: 0 mschar* longestPalindrome(char* s) { char *go,*max_start,*left,*right,*re; int max_length,temp_length; char *s_end; s_end = s+strlen(s); if (strlen(s) == 0) return s; if (strlen(s) == 1) return s; for(go = s,max_length = 1;go<s_end;) { if (s_end  go <= max_length * sizeof(char) / 2) break; left = go,right = go; while(*right==*(right+1)) right++; go = right + 1; while(left<s_end && left>s && *(left1) == *(right+1)) { right++; left; } temp_length = (rightleft)/sizeof(char) + 1; if(temp_length > max_length) { max_start = left; max_length = temp_length; } } re =(char *) malloc(max_length*sizeof(char)+1); go = re; while(max_length) { *(go) = *(max_start); go++; max_start++; max_length; } *go = '\0'; return re;
}

Brilliant!
I rewrite it using JAVA, runtime: 340ms
public String longestPalindrome(String s) { if (s.isEmpty()) return ""; if (s.length() == 1) return s; int min_start = 0, max_len = 1; for (int i = 0; i < s.length(); ) { if (s.length()  i <= max_len / 2) break; int j = i, k = i; while (k < s.length()  1 && s.charAt(k + 1) == s.charAt(k)) ++k; // Skip duplicate characters. i = k + 1; while (k < s.length()  1 && j > 0 && s.charAt(k + 1) == s.charAt(j  1)) { ++k; j; } // Expand. int new_len = k  j + 1; if (new_len > max_len) { min_start = j; max_len = new_len; } } return s.substring(min_start, min_start + max_len); }