如何在 JavaScript 中编写回文

IT技术 javascript palindrome
2021-03-14 14:56:00

我想知道如何在 javascript 中编写回文,在其中输入不同的单词,程序会显示单词是否为回文。例如,单词 noon 是回文,而 bad 则不是。

先感谢您。

6个回答
function palindrome(str) {

    var len = str.length;
    var mid = Math.floor(len/2);

    for ( var i = 0; i < mid; i++ ) {
        if (str[i] !== str[len - 1 - i]) {
            return false;
        }
    }

    return true;
}

palindrome如果指定的单词是回文,则根据boolean值(真/假)返回

更新:

由于性能,我对这个问题开放了赏金,我已经做了研究,结果如下:

如果我们正在处理非常大量的数据,例如

var abc = "asdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfd";

for ( var i = 0; i < 10; i++ ) {
    abc += abc;  // making string even more larger
}

function reverse(s) { // using this method for second half of string to be embedded
    return s.split("").reverse().join("");
}

abc += reverse(abc); // adding second half string to make string true palindrome

在这个例子中,回文是 True,只是要注意

发布的回文函数为我们提供了 180 到 210 毫秒的时间(在当前示例中),下面使用string == string.split('').reverse().join('')方法发布的函数为我们提供了 980 到 1010 毫秒的时间。

机器详情:

系统: Ubuntu 13.10 操作系统类型: 32 位 RAM: 2 Gb CPU: 3.4 Ghz*2 浏览器: Firefox 27.0.1

@crypticous 不,他的意思是你的回文函数有一半的时间在做冗余比较;比较第一后str.length/2字符到最后一个str.length/2字符,你已经知道,如果字符串是回文,因为你已经比上半年后半段。也就是说,您应该除以str.length2 以获得完全相同的结果,但速度要快两倍。
2021-04-16 14:56:00
@AleksiTorhamo 我知道你的意思,尽管我在成功定义整个字符串后计算了时间。我的意思是我在开始检查palindrome功能时直接计算时间
2021-04-27 14:56:00
您可以将一半存储在另一个变量中以避免每次迭代都计算它var half = Math.floor(len/2);然后在 forfor (var i = 0; i < half; i++)
2021-04-30 14:56:00
另请注意,在您的第一个示例中,您只需要遍历字符串长度的一半
2021-05-16 14:56:00
@w3d 是的,正是我的意思:)
2021-05-16 14:56:00

试试这个:

var isPalindrome = function (string) {
    if (string == string.split('').reverse().join('')) {
        alert(string + ' is palindrome.');
    }
    else {
        alert(string + ' is not palindrome.');
    }
}

document.getElementById('form_id').onsubmit = function() {
   isPalindrome(document.getElementById('your_input').value);
}

所以这个脚本会提示结果,是不是回文。您需要your_id使用您的输入 id 和form_id您的表单 id 更改 以完成这项工作。

演示!

你是对的,但我认为 OP 不需要兼容性,即像符号之类的非字母。正如他指出的例子“中午”。为什么您甚至想要支持符号?
2021-04-28 14:56:00
虽然这个解决方案很简单/漂亮,但是对于更大的数据集,组合==.reverse()只遍历字符串长度的一半不会更慢吗?
2021-05-03 14:56:00
比较 s == s^R 而不是查看具有相同长度的 s = uv 和 s = uav 的情况,将 u 和 v 减半,然后比较 u == v^R。
2021-05-10 14:56:00
我不一定在考虑通用的“符号”,而是考虑重音字母(例如 á、é、í、ó、ú、ü、ñ 等)和其他非拉丁字母 - 无论它们是否会成为问题或不是我不知道。但是,是的,只给出了英语示例。
2021-05-15 14:56:00
只是想知道由于 JavaScript 的内部字符编码,这里使用的字符串反转方法是否可能存在潜在问题?我怀疑这会是英语回文的问题,但是,在其他语言中可能更像是一个问题。只是需要注意的事情。stackoverflow.com/a/16776621/369434
2021-05-16 14:56:00

使用这样的东西

function isPalindrome(s) {
    return s == s.split("").reverse().join("") ? true : false;
}

alert(isPalindrome("noon"));

或者上面的代码可以优化为[在rightfold的评论后更新]

function isPalindrome(s) {
    return s == s.split("").reverse().join("");
}

alert(isPalindrome("malayalam")); 
alert(isPalindrome("english")); 
比较 s == s^R 也遭受双重努力,因为在回文 s = uu^R 的情况下,它将 uu^R 与 (uu^R)^R = (u^R)^Ru^R 进行比较= u^Ru,只需在中间拆分并将 u 与 u^R 进行比较就足够了。
2021-04-21 14:56:00
@rightfold 我会说它更冗长。您可以立即看到它将返回一个布尔值。
2021-04-29 14:56:00

更快的方式:

- 在循环中计算一半。

- 将单词的长度存储在变量中,而不是每次都计算。

编辑:将 word length/2 存储在临时变量中,以免在循环中每次都计算 (mvw) 所指出的。

function isPalindrome(word){
   var i,wLength = word.length-1,wLengthToCompare = wLength/2;

   for (i = 0; i <= wLengthToCompare ; i++) {
     if (word.charAt(i) != word.charAt(wLength-i)) {
        return false;
     }
   }
   return true;
} 
@Sai 有人在想 :),肯定会 +1。
2021-04-18 14:56:00
@BartekBanachewicz 你是对的,'length' 属性并不总是被计算出来,因此在某些浏览器上不会降低性能。但是,在某些浏览器上,缓存到变量比在循环中访问 'length' 属性更快。
2021-04-28 14:56:00
我很确定现代 JS 引擎可以优化.length访问
2021-05-03 14:56:00
@Sai 那么至少不应该说它最终更快,但我看到你已经完成了编辑。
2021-05-08 14:56:00
+1 表示遍历原始字符串长度的一半(其他答案似乎出于某种原因错过了这一点)。
2021-05-13 14:56:00

看这个:

function isPalindrome(word){
    if(word==null || word.length==0){
        // up to you if you want true or false here, don't comment saying you 
        // would put true, I put this check here because of 
        // the following i < Math.ceil(word.length/2) && i< word.length
        return false;
    }
    var lastIndex=Math.ceil(word.length/2);
    for (var i = 0; i < lastIndex  && i< word.length; i++) {
        if (word[i] != word[word.length-1-i]) {
            return false;
        }
     }
     return true;
} 

编辑:现在执行了一半的比较操作,因为我只迭代了半个字来将它与单词的最后一部分进行比较。大数据更快!!!

由于字符串是字符数组,因此无需使用 charAt 函数!!!

参考:http : //wiki.answers.com/Q/Javascript_code_for_palindrome

现在它做了一半的比较......你不能做得更少
2021-04-25 14:56:00