leetcode+ 判断是否回文串,只看是不是字母和数字,函数

点击打开链接
class Solution {
public:
    char tolower(char ch)
    {
        if(ch>=65 && ch<=90){
            ch = ch - 'A' +'a';
        }
        return ch;
    }
    bool isPalindrome(string s) {
        int left = 0, right = s.size()-1;
        while (left < right) {
            if(isalpha(s[left]) || isalnum(s[left])){
                if(isalpha(s[right]) || isalnum(s[right])){
                    if(tolower(s[left]) != tolower(s[right])){
                        return false;
                    }
                    else{ //配对啦
                        left++; right--;
                    }
                }
                else{
                    right--;
                }
            }
            else{
                left++;
            }
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/u013554860/article/details/80989031