leetcode-Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama" is a
palindrome."race a car" is not a palindrome.
Note:
Have you consider that the string might be empty? This is a
good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
class Solution { public: bool isPalindrome(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function //空串直接认为是回文 if (s.empty()) { return true; } //对字符串进行处理,过滤掉不需要处理的字符 string newS; for (string::size_type i = 0; i < s.size(); ++i) { if (isdigit(s[i]) || isalpha(s[i])) { newS.push_back(s[i]); } } //过滤后没有可供处理的字符,认为是回文 if (newS.empty()) { return true; } string::size_type first; string::size_type last; first = 0; last = newS.size() - 1; while (first < last) { if ((0 != newS[first] - newS[last]) && 32 != abs(newS[first] - newS[last])) { break; } ++first; --last; } return (first >= last)?true:false; } };

浙公网安备 33010602011771号