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.
思路:
这题不能更简单了,考察的是徒手写bug的能力= =
代码:
bool isPalindrome(string s) {
int n = s.length();
int i = ;
int j = n-;
while(i<j){//while(i>j){//蠢哭了
while(!isalnum(s[i]) && i<n) i++;//没加i<n会超时,不看题用isalpha()
while(!isalnum(s[j]) && j>=) j--;
if(i<j && tolower(s[i++]) != tolower(s[j--])) //if(s[i++] != s[j--] && i>j)//自加自减最坑了, 不看题没用tolower()
return false;
}
return true;
}