125. 验证回文串

125. 验证回文串


题目链接:125. 验证回文串

代码如下:

class Solution {
public:
    bool isPalindrome(string s) {
        vector<char> str;

        //把字符集中到容器中
        for (int i = 0; i < s.size(); i++) {
            if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z' ||
                s[i] >= '0' && s[i] <= '9') {
                if (s[i] >= 'A' && s[i] <= 'Z')
                    str.push_back(s[i] + 32);
                else
                    str.push_back(s[i]);
            }
        }

        //进行比较
        for (int i = 0; i < str.size() / 2; i++) {
            if (str[i] != str[str.size() - i - 1])
                return false;
        }

        return true;
    }
};

你可能感兴趣的:(leetcode,c++)