125. Valid Palindrome(js)

125. Valid Palindrome

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

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

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false
题意:给出一个句子,问所有单词组成的字符串是否是回文
代码如下:
/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function(s) {
        var len=s.length;
    var str="";
    for(var i=0;i<len;i++){
        if((s.charAt(i)>='a' && s.charAt(i)<='z') || (s.charAt(i)>='A' && s.charAt(i)<='Z')||(s.charAt(i)>='0' && s.charAt(i)<='9')){
            str+=s.charAt(i).toLowerCase();
        }
    }
    var strLen=str.length;
    for(var i=0;i<strLen;i++){
        if(str.charAt(i)!=str.charAt(strLen-1-i)){
            return false;
        }
    }
    return true;
};

猜你喜欢

转载自www.cnblogs.com/xingguozhiming/p/10864380.html