题目描述:
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.
bool Solution::isPalindrome(string s) { int head = 0; int tail = s.size()-1; while(head < tail) { while(!(isalpha(s[head]) || isalnum(s[head])) && head <= tail) head++; while(!(isalpha(s[tail]) || isalnum(s[tail])) && head <= tail) tail--; if(head < tail) { if((isalnum(s[head]) && isalnum(s[tail]) && s[head] == s[tail]) || isalpha(s[head]) && isalpha(s[tail]) && (s[head] == s[tail] || abs(s[head] - s[tail]) == 32)) { head++; tail--; } else return false; } } return true; }
原文地址:http://blog.csdn.net/yao_wust/article/details/40832147