给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: "A man, a plan, a canal: Panama"
输出: true
示例 2:
输入: "race a car"
输出: false
#include <iostream>
#include <string>
using namespace std;
/**
* @brief:LeetCode---判断回文
* @date:2020-3-15
*/
class solotion
{
public:
/*判断是否为回文*/
bool isPalindrom(string s) {
if(s.empty()){
return false ;
}
int start=0;
int end=s.size()-1;
while(start<end){
if(!isAlphanumeric(s.at(start))){
start++;
}
else if(!isAlphanumeric(s.at(end))){
end--;
}
else if(::tolower(s.at(start))!=::tolower(s.at(end))){
return false;
}
else{
++start;
--end;
}
}
return true;
}
/*判断是否为字符或者数字*/
bool isAlphanumeric(char c);
public:
bool res;
};
bool solotion::isAlphanumeric(char c)
{
if((c>='A'&&c<'Z')||(c>'a'&&c<'z')||(c>'0'&&c<'9')){
return true;
}
else
return false;
}
/**
* @brief main[主要是进行测试]
* @param argc:NULL 命令行参数的个数
* @param argv:NULL 主要是在数组中进行参数的存储
* @return
*/
int main(int argc, char *argv[])
{
solotion sol;
string str="aq,q 1a";
sol.res =sol.isPalindrom(str);
cout<<sol.res<<endl;
cout << "Hello World!" << endl;
return 0;
}