125. 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.

 

判断是不是回文串  只考虑字母和数字  忽略大小写和其他的

 

isalpha 字母(包括大写、小写)

islower(小写字母)

isupper(大写字母)

isalnum(字母大写小写+数字)

isblank(space和\t)

isspace(space、\t、\r、\n)

C++(9ms):

 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         int i = 0 ;
 5         int j = s.size() - 1 ;
 6         while(i < j){
 7             if (!isalnum(s[i])) i++ ;
 8             else if (!isalnum(s[j])) j-- ;
 9             else if (toupper(s[i++]) != toupper(s[j--]))
10                 return false ;    
11         }
12         return true ;
13     }
14 };

 

posted @ 2017-10-22 19:43  __Meng  阅读(140)  评论(0编辑  收藏  举报