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的能力= =
代码:
1 bool isPalindrome(string s) { 2 int n = s.length(); 3 int i = 0; 4 int j = n-1; 5 while(i<j){//while(i>j){//蠢哭了 6 while(!isalnum(s[i]) && i<n) i++;//没加i<n会超时,不看题用isalpha() 7 while(!isalnum(s[j]) && j>=0) j--; 8 if(i<j && tolower(s[i++]) != tolower(s[j--])) //if(s[i++] != s[j--] && i>j)//自加自减最坑了, 不看题没用tolower() 9 return false; 10 } 11 return true; 12 }