首页 > 代码库 > LeetCode:Valid Palindrome

LeetCode: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.

代码:

bool Solution::isPalindrome(string s)
{
    int head = 0;
    int tail = s.size()-1;
    while(head < tail)
    {
        while(!(isalpha(s[head]) || isalnum(s[head])) && head <= tail)
            head++;
        while(!(isalpha(s[tail]) || isalnum(s[tail])) && head <= tail)
            tail--;
        if(head < tail)
        {
            if((isalnum(s[head]) && isalnum(s[tail]) && s[head] == s[tail]) || isalpha(s[head]) && isalpha(s[tail]) && (s[head] == s[tail] || abs(s[head] - s[tail]) == 32))
            {
                head++;
                tail--;
            }
            else
                return false;
        }
    }
    return true;
}


LeetCode:Valid Palindrome