最长回文子串

问题描述 投票:0回答:2

我正在尝试通过 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

java substring palindrome
2个回答
0
投票

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


0
投票
var longestPalindrome = function(s) {
    let maxLength = 1;
    let startIndex = 0;

    function expandAroundMiddle(left, right) {

        while(left >= 0 && right < s.length && s[left] === s[right]) {
            const currentPalLength = right - left + 1;

            if(currentPalLength > maxLength) {
                maxLength = currentPalLength;
                startIndex = left;    
            }
            left -=1;
            right +=1;
        }
    }

    for(let i=0; i<s.length; i++) {
        expandAroundMiddle(i-1, i+1);
        expandAroundMiddle(i, i+1);
    }

    return s.slice(startIndex, startIndex + maxLength);
};
© www.soinside.com 2019 - 2024. All rights reserved.