Valid Palindrome
2015-03-16 15:32 笨笨的老兔子 阅读(128) 评论(0) 编辑 收藏 举报判断一个字符串是否是对称的
跳过所有非数字和字母,字母不分大小写
- 知道函数isalnum和tolower或者toupper就可以做了,两个指针一个指向头,一个指向尾,朝中间靠拢比较
空串是对称的
class Solution {
public:
bool isPalindrome(string s) {
if (s.empty())
{
return true;
}
int head = 0,tail=s.size()-1;
while (head < tail)
{
if (isalnum(s[head]) && isalnum(s[tail]))
{
if (tolower(s[head]) == tolower(s[tail]))
{
head++; tail--;
}
else
{
return false;
}
}
if (!isalnum(s[head]))
{
head++;
}
if (!isalnum(s[tail]))
{
tail--;
}
}
return true;
}
};