Leetcode 125 Valid Palindrome 字符串处理

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

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

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

class Solution {
public:
bool isPalindrome(string s) {
string::size_type j = ;
for(string::size_type i = ; 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(, j);
string str = t;
reverse(t.begin(),t.end());
return t == str;
}
};
上一篇:PHP自学4——通过函数将数组数据输出到html的Table标签中(使用函数的例子)


下一篇:深入学习golang(3)—类型方法