1.Question
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.
2.Codeclass Solution {
public:
bool isPalindrome(string s) {
if(s.empty()) return true;
int j = s.size() - 1;
int i = 0;
while(i < j)
{
char front = s[i], tail = s[j];
while((front >'z' || front < 'a') && (front > '9' || front < '0') && i < j)
{
if(front <= 'Z' && front >= 'A') front = front - 'A' + 'a';
else front = s[++i];
}
while((tail >'z' || tail < 'a') && (tail > '9' || tail < '0') && i < j)
{
if(tail <= 'Z' && tail >= 'A') tail = tail - 'A' + 'a';
else tail = s[--j];
}
if(i < j)
{
if(front != tail) return false;
else i++, j--;
}
}
return true;
}
};
3.Notea. 从两端往中间找字母数字,然后进行比较是否相等。终止条件是i >= j。
b. 如果找到的是大写字母则转换为小写字母。