LeetCode - Valid Palindrome

palindrome - 回文

alphanumeric - 字母数字

Have you consider that the string might be empty? This is a good question to ask during an interview.

Attention to :

1. only alphanumeric chars

2. ignore cases

3. empty string condition

 

Easy!

bool isalphanumeric(char c)
{
    if(c>='A'&&c<='z' || c>='0'&&c<='9')
        return true;
    else
        return false;
}

class Solution {
public:
    bool isPalindrome(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int len = s.size();
        if(len == 0) return true;
        int lp = 0;
        int rp = len-1;
        while(lp < rp)
        {
            while(lp<rp && !isalphanumeric(s[lp]))    lp++;
            while(lp<rp && !isalphanumeric(s[rp]))    rp--;
            if(tolower(s[lp]) != tolower(s[rp]))
                return false;
            else
            {
                lp++;rp--;
            }
        }
        return true;
    }
};

 

 

 

 

 

 

 

 

 

posted on 2013-04-16 20:06  highstar88  阅读(120)  评论(0编辑  收藏  举报

导航