class Solution {
public:
bool isPalindrome(string s) {
if(!s.size()) return true;
int head=0;
int tail = s.size()-1;
while(head<tail){
if(!isalnum(s[head])) {
//Check if character is alphanumeric.
head++;
continue;
}
if(!isalnum(s[tail])) {
tail--;
continue;
}
if(toupper(s[head++])!=toupper(s[tail--])) return false;
}
return true;
}
};
二刷
class Solution {
public:
bool isPalindrome(string s) {
if(s.length() == 0) {
return true;
}
int left=0;
int right=s.length()-1;
while(left<right){
while(!isalnum(s[left])) {
++left;
}
while(!isalnum(s[right])) {
--right;
}
if(lef t< right && toupper(s[left]) != toupper(s[right])) {
return false;
} else {
++left;
--right;
}
}
return true;
}
};