Palindrome检查Javascript [英] Palindrome check in Javascript

查看:98
本文介绍了Palindrome检查Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

function checkPalindrom(palindrom)
{

    for( var i = palindrom.length; i > 0; i-- )
    {
        if( palindrom[i] = palindrom.charAt(palindrom.length)-1 )
        {
            document.write('the word is palindrome.');
        }else{
            document.write('the word is not palindrome!');
        }
    }
}
checkPalindrom('wordthatwillbechecked');

我的代码有什么问题?我想检查这个词是否是回文。

What is wrong with my code? i want to check if the word is palindrome.

推荐答案

比标准答案快25倍



25x faster than the standard answer

function isPalindrome(s,i) {
 return (i=i||0)<0||i>=s.length>>1||s[i]==s[s.length-1-i]&&isPalindrome(s,++i);
}

使用类似:

isPalindrome('racecar');

因为它定义了i本身

小提琴: http://jsfiddle.net/namcx0yf/9/

这比下面的标准答案快〜25倍。

This is ~25 times faster than the standard answer below.

function checkPalindrome(str) {
  return str == str.split('').reverse().join('');
}

小提琴: http://jsfiddle.net/t0zfjfab/2/

查看控制台的性能结果。

View console for performance results.

虽然解决方案难以阅读和维护,但我建议您理解它以通过递归和位移来展示非分支,以打动您的下一位面试官。

Although the solution is difficult to read and maintain, I would recommend understanding it to demonstrate non-branching with recursion and bit shifting to impress your next interviewer.

||和&&用于控制流程,如ifelse。如果还剩下||是真的,它只是退出真实。如果||的某些内容是假的它必须继续下去。如果&&和如果是&&和是的,它必须继续下去。这被认为是非分支,因为它不需要if-else interupts,而只需要进行评估。

The || and && are used for control flow like "if" "else". If something left of || is true, it just exits with true. If something is false left of || it must continue. If something left of && is false, it exits as false, if something left of a && is true, it must continue. This is considered "non-branching" as it does not need if-else interupts, rather its just evaluated.

1。使用初始化程序不要求将i定义为参数。如果已定义,则将i分配给自身,否则初始化为0.始终为false,因此始终评估下一个OR条件。

1. Used an initializer not requiring "i" to be defined as an argument. Assigns "i" to itself if defined, otherwise initialize to 0. Always is false so next OR condition is always evaluated.

(i = i || 0) < 0

2。检查i是否中途退出但跳过检查中间的奇数。这里移位的比特就像2除以最低偶数除2的结果。如果是真的那么就假定它已经完成了回文。如果为false,则评估下一个OR条件。

2. Checks if "i" went half way but skips checking middle odd char. Bit shifted here is like division by 2 but to lowest even neighbor division by 2 result. If true then assumes palindrome since its already done. If false evaluates next OR condition.

i >= s.length >> 1

3。根据i从开头的char和end char进行比较最终作为邻居或中间人的邻居见面。如果错误退出并假设不是回文。如果true继续到下一个AND条件。

3. Compares from beginning char and end char according to "i" eventually to meet as neighbors or neighbor to middle char. If false exits and assumes NOT palindrome. If true continues on to next AND condition.

s[i] == s[s.length-1-i]

4. 再次调用自身将原始字符串作为s传递递归。由于此时肯定定义了i,因此它会预先递增以继续检查字符串的位置。返回表示是否回文的布尔值。

4. Calls itself again for recursion passing the original string as "s". Since "i" is defined for sure at this point, it is pre-incremented to continue checking the string's position. Returns boolean value indicating if palindrome.

isPalindrome(s,++i)



但是......



一个简单的for循环仍然是我想象的两倍快回答( 又称KISS原则

function fastestIsPalindrome(str) {
  var len = Math.floor(str.length / 2);
  for (var i = 0; i < len; i++)
    if (str[i] !== str[str.length - i - 1])
      return false;
  return true;
}

http://jsfiddle.net/6L953awz/1/

这篇关于Palindrome检查Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆