LeetCode_Valid Palindrome

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  check(char test, char & c)

    {

       if(test >= 'a' && test <= 'z')

       {

        c = test;

        return true;

       }else if(test >= 'A' && test <= 'Z'){

     

        c = test - 'A' + 'a';

        return true;

       }else if (test >= '0' && test <= '9'){

     

          c = test;

          return true;

       }

       return false;

    }

    bool isPalindrome(string s) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        int j = s.length();

        if(j-- == 0) return true;

        int i = 0; char head,tail;

        while(i<j){

            while(i<j){

                if(check(s[i],head)) break;

                i++;

            }

            if(i>=j) return true;

            

            while(i<j){

                if(check(s[j], tail)) break;

                j--;

            }

            if(i>=j) return true;

            if(head != tail ) return false;

            i++;j--;

        }

        return true ;

    }

};

 

你可能感兴趣的:(LeetCode)