标签:
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
代码如下:(超时)
1 public class Solution { 2 public String longestPalindrome(String s) { 3 if(s.length()==1||s.length()==0) 4 return s; 5 6 String t=" ",sub=" "; 7 int count=0,max=0; 8 char[] ss=s.toCharArray(); 9 for(int i=0;i<ss.length;i++) 10 { 11 if(ss.length-i+1<=max) 12 return t; 13 for(int j=ss.length-1;j>i;j--) 14 { 15 if(ss[i]==ss[j]){ 16 sub=s.substring(i,j+1); 17 count=j-i+1; 18 if(isPalindrome(sub)&&count>max) 19 { 20 t=sub; 21 max=count; 22 break; 23 } 24 } 25 } 26 } 27 return t; 28 29 } 30 public boolean isPalindrome(String a){ 31 char[] aa=a.toCharArray(); 32 int i=0,j=aa.length-1; 33 while(i<=j) 34 { 35 if(aa[i]==aa[j]) 36 {i++;j--;} 37 else return false; 38 } 39 return true; 40 } 41 }
5. Longest Palindromic Substring
标签:
原文地址:http://www.cnblogs.com/ghuosaao/p/5481258.html