My Leetcode journey, this chapter mainly completes the use of Java implementation algorithm. This is the 5th article longest palindromic Substring
All code Download: GitHub Link: GitHub link, click Surprise, write article is not easy, welcome everyone to take my article, and give useful comments, of course you can also pay attention to my GitHub;
1. Topic Description:
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.
2. My train of thought:
1. The longest palindrome string solution
2. Once you see the longest palindrome string, I think about it. Manacher algorithm
3. Algorithmic thinking: See http://blog.sina.com.cn/s/blog_3fe961ae0101iwc2.html
3. My AC Code
Packagecom.rlovep.string;/** * Longest palindromic Substring * My idea: * 1. Longest palindrome string solution * See the longest palindrome string I thought of the manacher algorithm * algorithm idea: Please see my reproduced blog * @author Peace * * * * Public class palindromicsubstring { PublicStringLongestpalindrome(String input) {intpos=0;Center position of the right palindrome substring intmaxright=0;right-most position of the right-most palindrome substring intMaxlength=0;//String maximum palindrome length intfinpos=0;intfinmaxright=0;//Add delimiterStringBuilder sb=NewStringBuilder (); Sb.append ("#"); for(intI=0; I<input.length (); i++) {Sb.append (Input.charat (i)); Sb.append ("#"); }//Get a string arrayInput=sb.tostring ();Char[] s= Input.tochararray ();intRl[] =New int[S.length];//Store maximum palindrome string radius for each location for(intI=0; i<s.length;i++) {if(I<maxright) {Rl[i]=math.min (rl[2*pos-i], maxright-i);//Core code}Else{rl[i]=1; } while((I-rl[i]) >=0&&//cannot exceed left side(I+rl[i]) <s.length&&//cannot exceed right side(S[i-rl[i]]==s[i+rl[i])) {rl[i]+=1; }//Update the right-most position of the right-most palindrome substring if((i+rl[i]-1) >maxright) {pos=i; maxright=i+rl[i]-1; }if(rl[i]>maxlength) {finpos=i; Finmaxright=maxright; Maxlength=rl[i]; }} input=input.substring (finpos+1-maxlength,finmaxright);returnInput.replace ("#",""); }}
Ok this chapter is introduced here from the Wpeace (blog.wpeace.cn)
Leetcode05-longest palindromic substring Java version