leetcode:验证回文串
[题目链接]
string func(string s)//先将大写转换成小写,并且去除空格等,只保留小写字母
{
string tmp;
string::iterator it = s.begin();
while (it != s.end())
{
//大写字母
if (*it <= 90 && *it>=65)//A-Z的ASCII码为65-90
{
tmp += *it + 32;//a-z的ASCII码为97-122
}
//小写字母
else if(*it <= 122 && *it>=97)
{
tmp += *it;
}
else if (*it <= 57 && *it >= 48)//数字0-9
{
tmp += *it;
}
it++;
}
return tmp;
}
class Solution {
public:
bool isPalindrome(string s) {
string s1 = func(s);//去除非字母元素,并将大写转换为小写
string::iterator it = s1.begin();
string::reverse_iterator rit = s1.rbegin();
while (it!=s1.end())
{
if (*it != *rit)
{
return false;
}
else
{
it++;
rit++;
}
}
return true;
}
};