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;
};