javascript - 寻找最长的回文

标签 javascript algorithm data-structures

我在修改它之后让它工作,但不清楚某些 -1 和 +1 的情况。我下面的代码以及我在评论中的问题。还在这里重述问题:

  1. 在“centeredPalindrome”辅助函数的return 语句中为什么是“left + 1”?是因为你在扩容但只关心“扩容中”的东西,而不在意外部的限制吗?
  2. 在同一个 return 语句中,为什么 right 而不是 right +1?是因为你在 while 条件下做的是“length”而不是“length-1”吗?
  3. 如果它是奇数,我们向左扩展一个额外的 - 为什么?是不是因为一个奇数的回文总是“开头多出一个”?

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)

最佳答案

In the return statement inside the "centeredPalindrome" helper function why is it "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? is it because you are doing "length" and NOT "length-1" in the while condition?

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

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). The right value is already where we want it, but the left value needs to be incremented by 1 to be correct.

"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"?

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

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)

关于javascript - 寻找最长的回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32913370/

相关文章:

javascript - 这个显示为列表但具有键值对的数据结构是什么?

javascript - 为什么 jQuery $(document).ready() 在验证失败后没有在 Rails 渲染 View 中触发?

javascript - 有人可以告诉我为什么这个 js 函数不能在加载时运行吗?

javascript - 无法将 .push() 子文档放入 Mongoose 数组

mysql - 需要优化的sql建议

c# - 是否有为 C# 实现的图形数据结构

javascript - "' null ' is null or not an object"IE 错误

algorithm - 坐标算法

algorithm - 如何按字典顺序对数字进行排序?

c++ - 在 C++ 中填充并返回读取的数据