Simple solution


  • 2
    Y

    find the next two chars need to be compared.
    compare these two ignoring cases.

    bool isValid(char c) {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
    }
    bool isPalindrome(string s) {
        int start = 0, end = s.size() - 1;
        while (start < end) {
            if (!isValid(s[start])) { start ++; continue; }
            if (!isValid(s[end])) { end --; continue; }
            if (s[start] != s[end] && abs(s[start] - s[end]) != 32) return false;
            start ++;
            end --;
        }
        return true;
    }

  • 0
    S

    What is the effect of the two continue?


  • 0
    W

    abs(s[start] - s[end]) != 32 sometime will be wrong like 'Q' = 81 '1'=49 :'Q'-'1' = 32 But they are not equal。


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.