Valid Palindrome 合法的回文串
原创
©著作权归作者所有:来自51CTO博客作者我想有个名字的原创作品,请联系作者获取转载授权,否则将追究法律责任
Valid Palindrome
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
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) {
int i,j,len;
len=s.length();
transform(s.begin(), s.end(), s.begin(), ::toupper);
for(i=0,j=len-1;i<j;i++,j--)
{
while(!(s[i]<='Z'&&s[i]>='A'||s[i]>='0'&&s[i]<='9'))
{
i++;
if(i>=j)
return true;
}
while(!(s[j]<='Z'&&s[j]>='A'||s[j]>='0'&&s[j]<='9'))
{
j--;
if(i>=j)
return true;
}
if(s[i]!=s[j])
return false;
}
return true;
}
};