题目
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例
示例 1:
输入: “A man, a plan, a canal: Panama”
输出: true
示例 2:
输入: “race a car”
输出: false
代码
class Solution {
public:
bool isPalindrome(string s)
{
int i = 0, n = 0;
for (int j = 0; s[j] != '\0'; j++)
n++;
for(i=0;i<n;i++)
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] += 32;
int low = 0, high = n - 1;
while (low <= high)
{
if (((s[low] >= 'a' && s[low] <= 'z')||(s[low]<='9'&&s[low]>='0')) && ((s[high] >= 'a' && s[high] <= 'z')||(s[high] <= '9' && s[high] >= '0')))
{
if (s[low] == s[high])
{
low++;
high--;
}
else
{
return false;
}
}
else
{
if (!(s[low] >= 'a' && s[low] <= 'z') && !(s[low] <= '9' && s[low] >= '0'))
low++;
if (!(s[high] >= 'a' && s[high] <= 'z') && !(s[high] <= '9' && s[high] >= '0'))
high--;
}
}
return true;
}
};