Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider that the string might be empty? This is a good question to ask during an interview. For the purpose of this problem, we define empty string as valid palindrome.
分析
题目的意思是,给定一个字符串,忽略非数字非字母字符,忽略大小写,判断其是否是个回文串。
* 这道题判断是否是回文串,有两个步骤: * 1、对字符串的预处理,题目要求只考虑字符串中的数字和字母,而且字母忽略大小写。 * 为了方便,我们可以新建一个临时vector将非数字和字母的字符过滤掉,同时将 * 大写转换成小写,统一处理字符。 * 2、从字符串起始位置到中间位置,逐次判断它的对称位置的字符是否一致,否那么 * 不是回文;反之如果都一致,那么就是回文。
Code
class Solution {
public:
/*
* 这道题判断是否是回文串,有两个步骤:
* 1、对字符串的预处理,题目要求只考虑字符串中的数字和字母,而且字母忽略大小写。
* 为了方便,我们可以新建一个临时vector将非数字和字母的字符过滤掉,同时将
* 大写转换成小写,统一处理字符。
* 2、从字符串起始位置到中间位置,逐次判断它的对称位置的字符是否一致,否那么
* 不是回文;反之如果都一致,那么就是回文。
*
* */
bool isPalindrome(string s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(s.size() < 2)
return true;
vector<char> middlestring;
for(int pos = 0; pos < s.size(); pos++)
{
if( (s[pos] >= '0'&&s[pos] <= '9')||(s[pos] >= 'a'&&s[pos] <= 'z'))
middlestring.push_back(s[pos]);
// 将大写转换为小写
else if((s[pos] >= 'A'&&s[pos] <= 'Z') )
middlestring.push_back(static_cast<char>(s[pos] + 'a' - 'A'));
}
bool ok = true;
for(int pos = 0; pos < middlestring.size()/2; pos++)
{
// 判断对称位置的字符是否相等
if(middlestring[pos] != middlestring[middlestring.size()-1-pos])
{
ok = false;
break;
}
}
return ok;
}
};