std::string longestPalindrome(std::string s) { int n = s.length( ); if (n == 0) { return ""; } std::string longest = s.substr(0, 1); // a single char itself is a palindrome for (int center = 0; center < n - 1; center++) // 循环每个中心 { // 以centerw为中心的最长回文串 std::string p1 = expandAroundCenter(s, center, center); if (p1.length() > longest.length()) { longest = p1; } // 以center和center + 1为中心的最长回文串 std::string p2 = expandAroundCenter(s, center, center + 1); if (p2.length() > longest.length()) { longest = p2; } } return longest; }
string longestPalindrome(string s) { if(s.length()==0 || s.length()==1) return s; string max, sub; for(int i=0; i<s.length()-1; i++){ sub = expandAroundCenter(i, i, s); if(sub.length() > max.length()) max = sub; sub = expandAroundCenter(i, i+1, s); if(sub.length() > max.length()) max = sub; } return max; }
string longestPalindrome(string s) { int n = s.length(); if (n == 0) return ""; string longest = s.substr(0, 1); // a single char itself is a palindrome for (int i = 0; i < n-1; i++) { string p1 = expandAroundCenter(s, i, i); if (p1.length() > longest.length()) longest = p1; string p2 = expandAroundCenter(s, i, i+1); if (p2.length() > longest.length()) longest = p2; } return longest; }
string longestPalindrome2(string s) { int n = s.size(); string result; if (s.empty()) return ""; string longest = s.substr(0, 1); for (int i = 0; i < n - 1; ++ i) { string s1 = expandAroundCenter(s, i, i); if (s1.size() > longest.size()) longest = s1; string s2 = expandAroundCenter(s, i, i + 1); if (s2.size() > longest.size()) longest = s2; } return longest; }
//第二种写法,从中心拓展 string longestPalindrome(string s){ int n=s.length(); if(n==0){ return ""; } string longest=s.substr(0,1); for(int i=0;i<n;i++){ string p1=expandAroundCenter( s,i,i);//围绕中心i使劲往两边走 if(p1.length()>=longest.length()){ longest=p1; } string p2=expandAroundCenter( s,i,i+1);//围绕中心i使劲往两边走 if(p2.length()>=longest.length()){ longest=p2; } } return longest; }
string longestPalindrome(string s) { if (s.empty()) { return ""; } int n = s.size(); int maxlen = 1, start = 0, curlen = 0; for(int i = 0; i < n-1; i++) { curlen = expandAroundCenter(s, i, i); if (curlen > maxlen) { maxlen = curlen; start = i - curlen/2; } curlen = expandAroundCenter(s, i, i+1); if (curlen > maxlen) { maxlen = curlen; start = i - (curlen-2)/2; } } string rtn (s, start, maxlen); return rtn; }
string LongestPalindromicSubstring::longestPalindrome(string s) { int n = s.size(); if (n == 0) return ""; string result = s.substr(0, 1); for (int i = 0; i < n - 1; i++) { string p1 = expandAroundCenter(s, i, i); if (p1.size() > result.size()) result = p1; string p2 = expandAroundCenter(s, i, i + 1); if (p2.size() > result.size()) result = p2; } return result; }
char * longestPalindrome(char * s) { if (s == NULL) return NULL; int len = strlen(s); int start_position = 0; int max_len = 0; int i = 0; for (; i<len; ++i) { int l0 = expandAroundCenter(s, len, i, i); int l1 = expandAroundCenter(s, len, i, i+1); l0 = l0>l1 ? l0 : l1; if (l0 > max_len) { max_len = l0; start_position = i - (l0-1)/2; } } char * result = (char *)malloc((max_len+1)*sizeof(char)); strncpy(result, &s[start_position], max_len); result[max_len] = '\0'; return result; }
string longestPalindrome(string s) { if( s.size() == 0 ) return NULL; int start = 0, end = 0; for( int i = 0; i < s.size(); i++ ) { int len1 = expandAroundCenter( s, i, i ); int len2 = expandAroundCenter( s, i, i + 1 ); int len = max( len1, len2 ); if( len > (end - start) ) { start = i - (len - 1)/2; end = i + len/2; } } return s.substr(start, end - start + 1); }