寻找最长的回文

时间:2015-10-02 18:17:25

标签: javascript algorithm data-structures

我在修补它之后开始工作了,但我不清楚其中的一些-1和+1正在进行中。我的代码以及我在评论中的问题。也在这里回答问题:

  1. 在“centeredPalindrome”辅助函数里面的return语句中,为什么它是“left + 1”?是因为你正在扩张,但只关心什么是“扩张”,而不是外部限制?
  2. 在同样的回复声明中,为什么正确而不正确+1?是因为你在while条件下做“长度”而不是“长度为1”?
  3. 如果是奇数,我们会向左扩展一个 - 为什么?是因为一个奇怪的回文总会在开头有“一个额外的”吗?
  4. var longestPalindrome = function(string){
      var length = string.length;
      var result = "";
    
      //helper function
      var centeredPalindrome = function(left, right){
        //while loop with conditions for it being a palindrome. iterate to left/right while those conditions are met:
        while(left>= 0 && right < length&& string[left] === string[right]){
          left--;
          right++;
        }
    
        //why right and not right + 1? is it because you are doing length (and NOT length -1) in the while loop?
        //why left + 1? Is that because you are expanding but only care about what's "in the expansion", not the outer limit?
        return string.slice(left + 1, right);
      }
    
      //iterate through the string and apply the helper function
    
      for (var i = 0; i < string.length; i++) {
        //handle case for it being odd or even
        var evenPal = centeredPalindrome(i, i);
        // if it is odd, we expand one extra to the left via "i -1" - why? is it because an odd palindrome will always have "one extra at the beggining"?
        var oddPal = centeredPalindrome(i-1, i);
    
    
        //overwrite the result with the longest one between them
        if(oddPal.length > result.length){
          result = oddPal;
        }
    
        if(evenPal.length > result.length){
          result = evenPal;
        }
      };
    
      //return the final result
      return result;
    }
    
    
    console.log(longestPalindrome("racecar"));
    
    // returns "racecar" if I change the return inside "centerPalindrome" to string.slice(left, right), this becomes:
    //"ra"
    console.log(longestPalindrome("radar")); // returns "radar"
    console.log(longestPalindrome("abba")); // returns "abba"
    

    最好根据@DrewGaynor命名这样的变量:

        var oddPal = centeredPalindrome(i - 1, i + 1);
        var evenPal = centeredPalindrome(i, i + 1);
    

    在奇数回文的情况下,你想看向中心的左侧和右侧,如下所示。

    var oddPal = centeredPalindrome(i - 1, i + 1);
            racecar
               ^
               |
               Center is one character because the string has an odd length (7 characters)
    

    在偶数回文的情况下,你想看看两个字符长的中心,为此,你需要考虑中心的额外长度。 你也可以为“左”而不是i + 1为“右”做i-1。 但你不想两者兼而有之,那时你会看到一个三字母的中心或者从左边开始的-1!

         var evenPal = centeredPalindrome(i, i + 1);
    
    
             abba
              ^^
              |
              Center is two characters because the string has an even length (4 characters)
    

1 个答案:

答案 0 :(得分:1)

  

在“centeredPalindrome”辅助函数里面的return语句中为什么它是“left + 1”?那是因为你在扩张,但只关心什么是“在扩张中”,而不是外部限制?

     

在同样的回复声明中,为什么正确而不正确+1?是因为你在while条件下做的是“长度”而不是“长度为1”?

leftright变量是子字符串的左右外边界。期望的输出是这些边界之间的所有内容。举例说明:

abcd_racecar-efgh
    ^       ^
    |       |
    |       Final value of "right" (12)
    Final value of "left" (4)

slice function的参数是起始索引(包括所谓输出开始的实际索引)和结束索引(不包括,因此索引紧跟在所需输出的结尾之后)。 right值已经是我们想要的值,但left值需要递增1才能正确。

"abcd_racecar-efgh".slice(5, 12); //Output is "racecar"
  

如果它是奇数,我们向左扩展一个 - 为什么?是因为一个奇怪的回文总会在开头有“一个额外的”吗?

这样做是因为如果它具有偶数长度,则回文的中心可以是两个字符,或者如果它具有奇数长度(实际上在我看来与变量名称相矛盾),则它可以是一个字符。举例说明:

racecar
   ^
   |
   Center is one character because the string has an odd length (7 characters)

 abba
  ^^
  |
  Center is two characters because the string has an even length (4 characters)