leetcode - Valid Palindrome

来源:互联网 发布:linux 网络学习 编辑:程序博客网 时间:2024/06/09 21:03

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(std::string s) {    std::string _s;    for (int i = 0; i < s.size(); ++i)    {    if(('a' <= s[i] && s[i] <= 'z') || ('A' <= s[i] && s[i] <= 'Z') || ('0' <= s[i] && s[i] <= '9'))    {    if('A' <= s[i] && s[i] <= 'Z')    s[i] += 32;    _s.push_back(s[i]);    }    }    std::string t(_s);    reverse(_s.begin(), _s.end());    if(t == _s) return true;    else return false;     }};


0 0
原创粉丝点击