Leetcode longest palindromic substring (medium)/java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.