题目:
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.length();
vector<char> v;
for (int i = 0; i < len; i++) {
if (isalnum(s[i])) {
v.push_back(s[i]);
}
}
int vs = v.size();
for (int i = 0; i < vs / 2; i++) {
if (tolower(v[i]) != tolower(v[vs - i - 1]))return false;
}
return true;
}
};
想法:
看清题,审清题意