题目描述:
给定一个字符串,判断其是否为一个回文串。只包含字母和数字,忽略大小写。样例:
“A man, a plan, a canal: Panama” 是一个回文。
“race a car” 不是一个回文。
- 代码如下:
class Solution {
public:
/**
* @param s A string
* @return Whether the string is a valid palindrome
*/
bool isPalindrome(string& s) {
// Write your code here
transform(s.begin(),s.end(),s.begin(),::tolower);
auto first = s.begin();
auto last = prev(s.end());
while(first<last){
if(!::isalnum(*first)) ++first;
else if (!::isalnum(*last)) --last;
else if( *first!=*last)
return false;
else {first++,last--;}
}
return true;
}
};