Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama" Output: true
Example 2:
Input: "race a car" Output: false
解题思路:判断是否为回文,空格和标点不算在内。从开始和串尾扫描,空格和标点跳过
class Solution {
public:
bool isPalindrome(string s) {
int len=s.size();
int i=0,j=len-1;
while(i<j){
while(isalnum(s[i])==false &&i<j){i++;}
while(isalnum(s[j])==false &&i<j){j--;}
if(tolower(s[i])!=tolower(s[j]))return false;
i++;
j--;
}
return true;
}
};