[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.

Solution:

class Solution {

public:

    bool isPalindrome(string s) {

        string ss = "";

        for(int i = 0;i < s.length();i++)

        {

          if(s[i] >= 'A' && s[i] <= 'Z')

            ss += s[i] - 'A' + 'a';

          else if(s[i] >= 'a' && s[i] <= 'z')

            ss += s[i];

            else if(s[i] >= '0' && s[i] <= '9')

                ss += s[i];

        }



        if(ss.length() < 2)

          return true;

        else

        {

          for(int i = 0;i < ss.length() / 2;i++)

          {

            if(ss[i] != ss[ss.length() - 1 - i])

              return false;

          }

          return true;

        }

    }



};

你可能感兴趣的:(LeetCode)