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.
C Solution:
bool isPalindrome(char* s) {
char *t = s + strlen(s) - 1;
char flag[256] = {0};
int i;
for (i = 0; i < 10; i++) flag['0' + i] = '0' + i;
for (i = 0; i < 26; i++) flag['a' + i] = flag['A' + i] = 'a' + i;
while (s < t) {
while (s < t && !flag[*s]) s++;
if (s == t) return true;
while (!flag[*t]) t--;
if (flag[*s++] != flag[*t--]) return false;
}
return true;
}
Summary:
- nothing to say
LeetCode: 125. Valid Palindrome
近期评论