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.
判断是否为回文串。
将字符串去其他符号,大写转小写
将逆置后的字符串与没有逆置的对比。
class Solution {
public:
bool isPalindrome(string s) {
string temp;
for(int i=0;i<s.size();i++)
{
if((s[i]>='a' && s[i]<='z') || (s[i]>='0' && s[i]<='9')) temp+=s[i];
if((s[i]>='A' && s[i]<='Z') ) temp+=s[i]-'A'+'a';
}
string temp2=temp;
reverse(temp.begin(),temp.end());
return temp==temp2;
}
};