Valid Palindrome ---- LeetCode 125

Posted on 2016-04-20 16:08  徐岩  阅读(112)  评论(0)    收藏  举报

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.

Solution 1:

class Solution 
{
public:
    bool isPalindrome(string s) 
    {
        int left = 0, right = s.length() - 1;
        while(left < right)
        {
            char a = tolower(s[left]), b = tolower(s[right]);
            if(!isalnum(a))
            {
                ++left;
                continue;
            }
            if(!isalnum(b))
            {
                --right;
                continue;
            }
            if(a != b) return false;
            
            ++left;
            --right;
        }
        return true;
        
    }
};