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.

class Solution {
public:
    bool isPalindrome(string s) {
        int sSize = s.size();
        int i=0,j=sSize-1;
        int diff = 'a'-'A';
        while(i<j){
            while((i<j) && !((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z') || (s[i]>='0' && s[i]<='9'))){i++;}
            while((i<j) && !((s[j]>='a' && s[j]<='z') || (s[j]>='A' && s[j]<='Z') || (s[j]>='0' && s[j]<='9'))){j--;}
            if(i>=j){
                return true;
            }
            if(s[i]==s[j] || s[i]==s[j]+diff || s[j] == s[i]+diff){
                i++;
                j--;
            }else{
                return false;
            }
        }
        return true;
    }
};

 

posted @ 2015-11-29 00:11  zengzy  阅读(159)  评论(0编辑  收藏  举报
levels of contents