leetcode-Valid Palindrome

来源:互联网 发布:有了网站源码怎么安装 编辑:程序博客网 时间:2024/04/28 10:34
问题描述:

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) {        if(s=="")            return true;        vector<char> vec;        for(int i=0;i<s.length();i++)        {            if(s[i]>='a'&&s[i]<='z')                vec.push_back(s[i]);            if(s[i]>='A'&&s[i]<='Z')                vec.push_back(s[i]+32);            if(s[i]>='0'&&s[i]<='9')                vec.push_back(s[i]);        }        for(int j=0;j<vec.size()/2;j++)        {            if(vec[j]!=vec[vec.size()-j-1])                return false;        }        return true;    }};

0 0
原创粉丝点击