125. Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

这道题细心就好了:

var isPalindrome = function(s) {
    if (s==="")
        return true;
    var num = s.length;
    var p1 = 0;
    var p2 = num-1;
    while (p1<p2) {
        var a = s.charCodeAt(p1);
        var b = s.charCodeAt(p2);
        if (!((a>=48&&a<=57)||(a>=97&&a<=122)||(a>=65&&a<=90))) {
            p1++;
            continue;
        }
        if (!((b>=48&&b<=57)||(b>=97&&b<=122)||(b>=65&&b<=90))) {
            p2--;
            continue;
        }
        var temp = a-b;
        if (temp===0){
            p1++;
            p2--;
            continue;
        }
        if ((a>=97&&a<=122)||(b>=97&&b<=122)) {
            if (temp===32||temp===-32) {
                p1++;
                p2--;
                continue;
            }
        }
        return false;
    }
    return true;
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容