Longest back text string
Top title:
In the test case, note the aaabaaaa.
But I'm time limit exceeded. The solution was in a very violent way. (three-layer loop) find the longest text string for each character.
1 /**2 * Longest palindrome substring3 * 2017-5-74 **/5 6 ImportJava.io.*;7 ImportJava.util.*;8 Importjava.lang.*;9 Ten Public classSolution One { A Public Staticstring Longestpalindrome (string s) - { - intlen=s.length (); the Char[] c=S.tochararray (); -String str=NewString ("Hz"); - intMax=0; - intFrom=0,to=0; + intI,J,II,JJ; - for(i=0;i<len-1;i++) + { AIi=i; at for(j=len-1;j>0;j--) - { -jj=J; - while(c[i]==c[j]&&i<j-1) - { -i++;j--; in } - //System.out.println ("I:" +i+ "--j:" +j); to if(((i==j| | (i+1==j&& (C[i]==c[j]))) &&jj-ii>max)) + { -max=jj-II; theFrom=II; *to=JJ; $ //System.out.println (max+ "--" +ii+ "--" +jj ");Panax Notoginseng } -I=II; thej=JJ; + A } the } + //System.out.println (from+ "--" +to); -Str=s.substring (from,to+1); $ returnstr; $ - } - Public Static voidMain (string[] args) the { - WuyiSystem.out.println (Longestpalindrome ("Aaabaaaa")); the } - Wu}View Code
Hang on to the super long use case.
Decide to use dynamic planning.
"Pending Update"
Leetcode longest palindromic substring (medium)/java