[LeetCode] Valid Palindrome

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.

Easy题不easy啊,我提交了好几次都没过,各种case都没有考虑清楚就开始做了。两个pointers这个我倒是一上来就确定了。

  1.没有考虑清楚边界,其实两个指针往分两头往中间扫,当他们彼此超越的时候就应该结束循环。因为两个指针相互交错的时候如果还没有检测到不匹配的字符那肯定是回文串无误。

  2.没读懂题目,不造alphanumberic是”数字和字母组成“的意思。。。

bool isAlphanumeric(char c) {
    return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
}

bool isPalindrome(string s) {
    int i = 0 ,j = (int)s.length()-1;
    while (i <= j) {
        if (!isAlphanumeric(s[i])) {
            i++;
            continue;
        }
        if (!isAlphanumeric(s[j])) {
            j--;
            continue;
        }
        if (tolower(s[i]) != tolower(s[j])) {
            return false;
        }
        i++;
        j--;
    }
    return true;
}
原文地址:https://www.cnblogs.com/agentgamer/p/4085045.html