Leetcode 125 Valid Palindrome 字符串处理

题意:判断字符串是否是回文字符串

先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同。

该题最好的解法可以模仿 Leetcode 345 Reverse Vowels of a String 字符串处理 

class Solution {
public:
    bool isPalindrome(string s) {
        string::size_type  j = 0;
        for(string::size_type i = 0; i< s.size(); ++i){
            if(isalpha(s[i]) || isdigit(s[i])) {
                if(isupper(s[i])) s[j++] = s[i] - 'A' + 'a';
                else s[j++] = s[i] ;
            }
        }
        string t = s.substr(0, j);
        string str = t;
        reverse(t.begin(),t.end());
        return t == str;
    }
};
原文地址:https://www.cnblogs.com/onlyac/p/5464964.html