简体   繁体   English

寻找最长的回文

[英]Finding the longest palindrome

I got this to work after tinkering with it, but am unclear on some of the -1s and +1s going on.我在修补它后让它工作,但不清楚一些 -1s 和 +1s 正在发生。 my code below as well as my questions in the comments.我下面的代码以及我在评论中的问题。 Also recapping the questions up here:同样在这里回顾一下问题:

  1. In the return statement inside the "centeredPalindrome" helper function why is it "left + 1"?在“中心回文”辅助函数中的返回语句中,为什么它是“left + 1”? Is that because you are expanding but only care about what's "in the expansion", not the outer limit?那是因为您正在扩展但只关心“扩展中”的内容,而不是外部限制吗?
  2. In that same return statement, why right and not right +1?在同一个 return 语句中,为什么正确而不是正确 +1? is it because you are doing "length" and NOT "length-1" in the while condition?是因为你在 while 条件下做“长度”而不是“长度-1”吗?
  3. if it is odd, we expand one extra to the left - why?如果它是奇怪的,我们向左扩展一个额外的 - 为什么? is it because an odd palindrome will always have "one extra at the beginning"?是不是因为一个奇怪的回文总是“一开始就有一个额外的”?

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"

probably better to name the variables like this per @DrewGaynor:可能更好地按照@DrewGaynor 命名这样的变量:

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

in the case of the odd palindrome, you want to look to the left and to the right of the center, as below.在奇数回文的情况下,您需要查看中心的左侧和右侧,如下所示。

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

in the case of the even palindrome, you want to look at the center which is two chars long, to do this, you need to account for the extra length of the center.在偶数回文的情况下,您想查看两个字符长的中心,为此,您需要考虑中心的额外长度。 you could have also done i-1 for the "left" instead of i+1 for the "right".你也可以为“左”做 i-1 而不是为“右”做 i+1。 but you don't want to do it for both since then you will be looking at a three letter center or starting the left at -1!但是您不想同时为两者都这样做,因为那时您将看到三个字母的中心或从 -1 开始左侧!

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


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

In the return statement inside the "centeredPalindrome" helper function why is it "left + 1"?在“中心回文”辅助函数中的返回语句中,为什么它是“left + 1”? Is that because you are expanding but only care about what's "in the expansion", not the outer limit?那是因为您正在扩展但只关心“扩展中”的内容,而不是外部限制吗?

In that same return statement, why right and not right +1?在同一个 return 语句中,为什么正确而不是正确 +1? is it because you are doing "length" and NOT "length-1" in the while condition?是因为你在 while 条件下做“长度”而不是“长度-1”吗?

The left and right variables are the left and right outer bounds of the substring.leftright变量是字符串的左和右外边界。 The desired output is everything between those bounds.所需的输出是这些界限之间的一切。 To illustrate:为了显示:

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

The arguments of the slice function are the start index (inclusive, so the actual index of the start of the desired output) and end index (exclusive, so the index immediately following the end of the desired output). slice 函数的参数是开始索引(包括,因此是所需输出开始的实际索引)和结束索引(不包括,因此是紧跟在所需输出结束之后的索引)。 The right value is already where we want it, but the left value needs to be incremented by 1 to be correct. right值已经在我们想要的位置,但left值需要增加 1 才能正确。

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

If it is odd, we expand one extra to the left - why?如果它是奇怪的,我们向左扩展一个额外的 - 为什么? is it because an odd palindrome will always have "one extra at the beginning"?是不是因为一个奇怪的回文总是“一开始就有一个额外的”?

This is done because the center of the palindrome could be two characters if it has an even length, or one character if it has an odd length (which actually seems to me to contradict the variable names).这样做是因为回文的中心可能是两个字符,如果它有偶数长度,或者一个字符,如果它有奇数长度(这在我看来实际上与变量名称矛盾)。 To illustrate:为了显示:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM