问题描述:
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.
分析:注意过滤就好了。
代码如下:12ms
class Solution {
public:
char getValid(char s){
if(s>='A'&&s<='Z')
return s+32;
else if(s>='a'&&s<='z')
return s;
else if(s>='0'&&s<='9')
return s;
else
return 0;
}
bool isPalindrome(string s) {
int length = s.size();
int start = 0;
int end = length-1;
while(start<end){
char sa = s[start];
char ea = s[end];
char ss = getValid(sa);
if(!ss){
start++;
continue;
}
char ee = getValid(ea);
if(!ee){
end--;
continue;
}
if(ss==ee){
start++;
end--;
}else{
return false;
}
}
return true;
}
};