LeetCode-Valid Palindrome

来源:互联网 发布:mac能用matlab吗 编辑:程序博客网 时间:2024/04/28 15:27
class Solution {public:    bool isPalindrome(string s) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        bool ans = true;        int beg = 0;        int end = s.size() - 1;        while (beg < end)        {            while (!isalnum(s[beg]) && beg < end)                ++beg;            while (!isalnum(s[end]) && beg < end)                --end;            if (beg < end)            {                if (isSame(s[beg], s[end]))                {                    ++beg;                    --end;                }                else                {                    ans = false;                    break;                }            }        }        return ans;    }        bool isSame(char ch1, char ch2)    {        if (isdigit(ch1) && isdigit(ch2))            return ch1 == ch2;        else if (isalpha(ch1) && isalpha(ch2))            return ch1 == ch2 || abs(ch1 - ch2) == 32;        else            return false;    }};