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.
注意空字符串也是一个回文!由两个指针i,j分别从前和从后移动,比较字符是否相同。
class Solution {
public:
bool isPalindrome(string s) {
int i, j;
i = 0, j = s.length() - 1;
while (i<j)
{
while (!judge(s[i])&&s[i]!='\0')
i++;
while (!judge(s[j])&&j>=1)
j--;
if(i>j)
break;
if (s[i] != s[j] && fabs(double(s[i] - s[j])) != 32)
return false;
i++;
j--;
}
return true;
}
bool judge(char c)
{
if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z' || c >= '0'&&c <= '9')
return true;
else
return false;
}
};