【LeetCode】125. Valid Palindrome

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.

左右分别往中间扫描,如果不同则返回false,如果两端碰面了则返回true

代码里写了很多注释,我认为这类题难点在于循环的判断条件以及跳出条件

因此对于每一个循环都需要明确它的进入条件以及跳出条件。

class Solution {
public:
    bool alphanumeric(char c)
    {
        if((c >= 'a' && c <= 'z') ||
           (c >= 'A' && c <= 'Z') ||
           (c >= '0' && c <= '9'))
            return true;
        else
            return false;
    }
    bool isPalindrome(string s) {
        if(s == "")
            return true;
        int i = 0;
        int j = s.size()-1;
        while(i < j)
        {
            while(!alphanumeric(s[i]))
                i ++;
            while(!alphanumeric(s[j]))
                j --;
            if(i < j && tolower(s[i]) != tolower(s[j]))
                return false;
            i ++;
            j --;
        }
        return true;
    }
};

原文地址:https://www.cnblogs.com/ganganloveu/p/4077246.html