【两根指针——相向双指针】Lintcode 415. 有效回文串

Lintcode 415. 有效回文串

题目描述:给定一个字符串,判断其是否为一个回文串。只考虑字母和数字,忽略大小写。
在这里插入图片描述

class Solution {
    
    
public:
    /**
     * @param s: A string
     * @return: Whether the string is a valid palindrome
     */
    bool isPalindrome(string &s) {
    
    
        for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
    
    
            while (!isdigit(s[i]) && !isalpha(s[i]) && i < j) {
    
    
                ++i;
            }
            while (!isdigit(s[j]) && !isalpha(s[j]) && i < j) {
    
    
                --j;
            }
            if (i < j && tolower(s[i]) != tolower(s[j])) {
    
    
                return false;
            }
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/phdongou/article/details/113838158