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.

 

字符串为空时判断为回文,大小写不区分可确定相等,数字与字母不同。

 



1 class Solution {
2 public:
3 bool isPalindrome(string s) {
4 int n=s.length();
5 if(n==0) return true;
6 int i=0,j=n-1;
7 while(i<j){
8 if(!isCharacters(s[i])){
9 i++;
10 continue;
11 }
12 if(!isCharacters(s[j])){
13 j--;
14 continue;
15 }
16
17 int left=0,right=0,leftsig=0,rightsig=0;
18 left=(s[i]>='0'&&s[i]<='9')?s[i]-'0':((s[i]>='a'&&s[i]<='z')?s[i]-'a':s[i]-'A');
19 right=(s[j]>='0'&&s[j]<='9')?s[j]-'0':((s[j]>='a'&&s[j]<='z')?s[j]-'a':s[j]-'A');
20 leftsig=(s[i]>='0'&&s[i]<='9')?0:1;
21 rightsig=(s[j]>='0'&&s[j]<='9')?0:1;
22 if(left!=right||leftsig!=rightsig)
23 return false;
24 i++;
25 j--;
26 }
27 return true;
28 }
29 bool isCharacters(char c){
30 if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))
31 return true;
32 else
33 return false;
34 }
35 };