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.

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 isA(char c){
        return c>='a'&&c<='z'||c>='A'&&c<='Z'||c>='0'&&c<='9';
    }
    char lower(char c){
        if(c>='A'&&c<='Z')return c-'A'+'a';
        return c;
    }
    bool isPalindrome(string s) {
        
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(s.length()<=1)return true;
        int start=0,end=s.length()-1;
        while(true){
            while(start<s.length()&&!isA(s[start])){
                start++;
            }
            
            while(end>=0&&!isA(s[end])){
                end--;
            }
            if(start>end)break;
            if(lower(s[start])!=lower(s[end]))return false;
            start++;
            end--;
        }
        return true;
    }
};

 

posted @ 2013-09-20 02:05  懒猫欣  阅读(187)  评论(0编辑  收藏  举报