【LeetCode】125. Valid Palindrome

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.

判断是否为回文字符串

【思路】提取数字和字母,大写字母全部转换成小写,存入vector中,从头尾遍历vector判断是否相等。

class Solution {
public:
    bool isPalindrome(string s) {
        if(s.size()==0) return true;
        vector<int> v;
        int len = s.size();
        for(int m=0; m < s.size();m++)
        {
            if(s[m]<='z'&&s[m]>='a'||s[m]<='9'&&s[m]>='0')
                v.push_back(s[m]);
            if(s[m]<='Z'&&s[m]>='A')
               v.push_back(s[m]+32);
        }
        for(int i =0; i < v.size()/2; i++)
        {
            if(v[i]!=v[v.size()-1-i])
                return false;
        }
        return true;
    }
};


你可能感兴趣的:(【LeetCode】125. Valid Palindrome)