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 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)
{
    int start=0,end=s.length()-1;
	while(start<end)
	{
		while(start<end&&!isChar(s[start]))
			++start;
		while(start<end&&!isChar(s[end]))
			--end;
		if (start<end)
		{
			if (!equal(s[start],s[end]))
				return false;
			++start;
			--end;
		}
	}
	return true;
}
bool isChar(char c)
{
	return (c>='0'&&c<='9')||(c>='a'&&c<='z')||(c>='A'&&c<='Z');
}
bool equal(char a,char b)
{
	if (a>='0'&&a<='9')
		return a==b;
	if (a>='A'&&a<='Z')
		a=a-'A'+'a';
	if (b>='A'&&b<='Z')
		b=b-'A'+'a';
	return a==b;
}

};


 

你可能感兴趣的:(Valid Palindrome)