#leetcode 125

I wrote this problem on 4 days ago, and today i write it again, but i find

that i can not write it once-time by myself, and i watch the video about t-

his problem again.

This problem description:

class Solution {
public:
    bool isAlphanumberic(char s)
    {
        return (s >= '0' && s <= '9') || (s >= 'A' && s <= 'Z') || (s >= 'a' && s <= 'z');
    }
    bool isLowercase(char i, char j)
    {
        if (i >= 'A' && i <= 'Z') i += 32;
        if (j >= 'A' && j <= 'Z') j += 32;
        return i == j;
    }
    bool isPalindrome(string s) {
        if (s.size() == 0)
            return true;
        
        int i = 0;
        int j = s.size() - 1;

        for (; i < j; ++i, --j)
        {
            while (i < j && !isAlphanumberic(s[i])) ++i;
            while (i < j && !isAlphanumberic(s[j])) --j;
            if (!isLowercase(s[i], s[j])) return false;
        }
        return true;
    }
};

This problem main point is that compare front and back item(letter or number) whether equal.

Firstly, if the item is not letter or number, move the index to next item.

Secondly, compare them, if they are not equal, exit the funtion.

posted @ 2022-02-02 00:41  越菜越自信  阅读(32)  评论(0)    收藏  举报