1,输入Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
2, 空字串和单个字符返回TRUE
3, 去除空格,同时把大写字母转化成小写
4, 二指针依次从左右夹逼比较,
bool isPalindrome(char* s) {
int len = strlen(s);
if(len == 0)
return true;
int j = 0;
//刨除掉非字母,同时把所用字符保存为小写模式
for(int i = 0; i < len ; i++){
if(isalnum(s[i]))
s[j++] = tolower(s[i]);
}
s[j] = '\0';
if(j == 1)
return true;
int l = 0;
int r = j-1;
//l+1 很有技巧,对于奇数,退出时候l = r, 对于偶数 l + 1 = r
while(l+1<r){
if(s[l]!=s[r])
return false;
l++;
r--;
}
if(s[l]==s[r])
return true;
else
return false;
}