LeetCode:125. 验证回文串

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。

示例 1:

输入: "A man, a plan, a canal: Panama"
输出: true

示例 2:

输入: "race a car"
输出: false
class Solution {
    bool isNumOrChar(char c){
        if(tolower(c) >= 'a' && tolower(c) <= 'z' || tolower(c) >= '0' && tolower(c) <= '9')
            return true;
        return false;
    }
public:
    bool isPalindrome(string s) {
        int n = s.size();
        if(n == 0) return true;
        int low = 0, high = n - 1;
        while(low < high){
            if(!isNumOrChar(s[low])) low ++;
            if(!isNumOrChar(s[high])) high --;
            if(isNumOrChar(s[low]) && isNumOrChar(s[high])){
                if(tolower(s[low]) == tolower(s[high])){
                    low ++; high --;
                }else return false;
            }
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_40513792/article/details/106222855
今日推荐