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.
Solution: traverse from both side.
1 #include <cctype> // needed for isalnum(), isupper() and tolower(). 2 class Solution { 3 public: 4 bool isPalindrome(string s) { 5 for(int i = 0, j = s.size()-1; i < j; i++, j--) { 6 while(i < j && !isalnum(s[i])) i++; 7 while(i < j && !isalnum(s[j])) j--; 8 if(tolower(s[i]) != tolower(s[j])) return false; 9 } 10 return true; 11 } 12 };
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号