valid-palindrome

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zslngu/article/details/81702890

题目描述

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
“A man, a plan, a canal: Panama”is a palindrome.
“race a car”is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

class Solution {
public:
    bool notAlpha(char c){
         if((64<c&&c<91)||(96<c&&c<123)||(c>47&&c<58))
            return false;
        return true;
    }
    bool isPalindrome(string s) {
        if(s.length()==0)
            return true;
        if(s.length()==1)
            return true;
        int p=-1;
        int q=s.length();


        while(p<q){
           while(notAlpha(s[++p]));
           while(notAlpha(s[--q]));
          if(p>=q)
                return true;   
           if(s[p]!=s[q] && s[p]-s[q]!=32 && s[q]-s[p]!=32)
               return false;
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/zslngu/article/details/81702890
今日推荐