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) {
        if(s.size()==0) return true;
        transform(s.begin(), s.end(), s.begin(), ::tolower);
        int l=0,r=s.size()-1;
        while(r>l){
            while(r>=0&&((s[r]>'z'||s[r]<'a')&&(s[r]>'9'||s[r]<'0'))) r--;
            while(l<s.size()&&((s[l]>'z'||s[l]<'a')&&(s[l]>'9'||s[l]<'0'))) l++;
            if(r<=l) return true;
            else{
               if(s[r]!=s[l]) return false;
                r--;
                l++; 
            } 
        }
        return true;
    }
};


你可能感兴趣的:(LeetCode,算法)