Given a string S, find the longest palindromic substring in s. The maximum length of S is assume, and there exists one unique longest palindromic substring.
Play for two days dota2, sin sin, or should honestly brush the question ah.
The problem is the longest palindrome string, which is used in the solution of violence, that is, to solve the two palindrome "Asdsa" and "Assa", that is, odd and even palindrome, equivalent to using a double cycle, the code is as follows:
1 classSolution {2 Public:3 stringLongestpalindrome (strings) {4 if(!s.size ())return "";5 stringret;6 stringtmp;7 for(inti =0; I < s.size (); ++i) {8TMP = Findpal (s, I-1, i +1);9 if(Tmp.size () >ret.size ())TenRET =tmp; OneTMP = Findpal (s, I, i +1); A if(Tmp.size () >ret.size ()) -RET =tmp; - } the returnret; - } - - stringFindpal (string& S,intLeftintRight ) + { - if(Left <0) + returnS.substr (left +1,1); A if(Right >=s.size ()) at returnS.substr (Right-1,1); - - while(Left >=0&& Right <s.size ()) { - if(S[left]! =S[right]) - Break; -left--; inright++; - } toleft++; + returnS.substr (left, right-Left ); - } the};
Leetcode oj:longest palindromic Substring (longest back text string)