最长的回文子字符串

时间:2015-11-12 20:28:09

标签: java substring palindrome

我正在尝试从leet代码解决问题。我已经为此写了一个方法。这在本地Eclipse中完美运行,但是当我以leetcode提交此解决方案时,它表示超出了时间限制。

有人可以向我推荐一些我可以在下面的代码中修改的内容,以使其更快地运行吗?我也可以在这篇文章中输入输入字符串。

代码:

public String longestPalindrome(String s) {
   if(s.equals("")) return "";
   if(s.length()==1) return s;
   if(s.length()==2) {
       if(s.charAt(0) == s.charAt(1))
           return s;      
   }

   char[] ch = s.toCharArray();
   Set<Integer> set = new HashSet<Integer>();
   int maxP=0;String maxPalin="";

   for(int i =2;i < s.length();i++){
       if((ch[i-1]==ch[i] || ch[i-2] == ch[i]) && !set.contains(i) ){
           int loop;
           if(ch[i-1]==ch[i]){
               loop=i-1;
           }
           else{
               loop=i-2;
           }

           for(int k =i,l=loop; l>=0 && k<s.length();k++,l--){
               if(ch[k]!= ch[l]){
                   if(maxP < s.substring(l+1, k).length())
                   maxPalin=s.substring(l+1, k);
                   maxP=s.substring(l+1, k).length();
                   set.add(i);
                   i=2;
                   break;
               }
               if(l==0){
                   if(maxP < s.substring(0, k+1).length())
                       maxPalin=s.substring(0, k+1);
                       maxP=s.substring(0, k+1).length();
                       set.add(i);
                       i=2;
                       break;
               }
               if(k== s.length()-1){
                   if(maxP < s.substring(l, s.length()).length())
                   maxPalin=s.substring(l, s.length());
                   maxP=s.substring(l, s.length()).length();
                   set.add(i);
                   i=2;
                   break;
               }
           }
       }
   }

   return maxPalin;
}

输入:

  

ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg

1 个答案:

答案 0 :(得分:0)

遍历字符,对于所有位置检查下一个字符是否与当前字符相同(例如'f','f')或后面的字符和之前的字符是否相同(例如'f',' G”, 'F')。 如果是这种情况,则前后循环并比较字符,直到找到两个不匹配的字符。 将字符串的长度与您到目前为止找到的最长字符串进行比较,如果它更长,则将其保存。 转到下一个字符。 如果找到长度/ 2大于剩余字符数的回文,也可以退出循环。