LeetCode: Valid Palindrome

思路:这个问题比较简单,设置头尾两个下标变量,如果相应的字符不是数字或者字母,就前(后)移下标,如果是,就比较是否一致,不一致,返回false。

class Solution {
public:
    bool isPalindrome(string s) {
        int len = s.length();
        if(len == 0)return true;
        int i = 0, j = len - 1;
        while(i < j){
            if((isdigit(s[i]) || isalpha(s[i])) && (isdigit(s[j]) || isalpha(s[j]))){
                if(s[i] == s[j] || abs(s[i] - s[j]) == 32){
                    i++;
                    j--;
                }
                else
                    return false;
            }
            else if(!isdigit(s[i])&&!isalpha(s[i]))
                    i++;
                else
                    j--;
        }
        return true;
    }
};


你可能感兴趣的:(LeetCode: Valid Palindrome)