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 ...
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 }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:5. Longest Palindromic Substring
关键词:string
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。