Lintcode 415. 有效回文串
题目描述:给定一个字符串,判断其是否为一个回文串。只考虑字母和数字,忽略大小写。
class Solution {
public:
/**
* @param s: A string
* @return: Whether the string is a valid palindrome
*/
bool isPalindrome(string &s) {
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
while (!isdigit(s[i]) && !isalpha(s[i]) && i < j) {
++i;
}
while (!isdigit(s[j]) && !isalpha(s[j]) && i < j) {
--j;
}
if (i < j && tolower(s[i]) != tolower(s[j])) {
return false;
}
}
return true;
}
};