Valid Palindrome

来源:互联网 发布:淘宝阅读官网 编辑:程序博客网 时间:2024/04/23 19:38

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) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        string temp;        int n = s.size();        for(int i = 0; i < n; ++i){            char &c = s[i];            if(c >='a' && c <= 'z'){                temp += c;            }            else if(c >= 'A' && c <= 'Z'){                temp += (c- 'A' + 'a');            }            else if(c >= '0' && c <= '9'){                temp += c;            }        }        n = temp.size();        if(!n) return true;        string rev = temp;        rev.assign(temp.rbegin(), temp.rend());        if(temp == rev) return true;        return false;    }};


原创粉丝点击