Valid Palindrome

Q:

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.

A:

easy,注意大小写转换,数字为合法字符。即可

class Solution {
public:
    bool isPalindrome(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        bool result = true;
        int front = 0;
        int end = s.size() - 1;
        while (front <= end) {
            if (!transferChar(&s[front])) {
                front++;
            } else if (!transferChar(&s[end])) {
                end--;
            } else {
                if (s[front] != s[end]) return false;
                front++;
                end--;
            }
        }
        return result;
    }
    
    bool transferChar(char* c) {
        if ((*c >= 'a' && *c <= 'z') || (*c >= '0' && *c <= '9')) {
            return true;
        }
        if (*c >= 'A' && *c <= 'Z') {
            *c = 'a' + *c - 'A';
            return true;
        }
        return false;
    }
};

 

posted @ 2013-07-03 23:56  dmthinker  阅读(95)  评论(0)    收藏  举报