LeetCode | 125. Valid Palindrome

 

题目:

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

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

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false

 

Constraints:

  • s consists only of printable ASCII characters.

 

代码:

class Solution {
public:
    bool isPalindrome(string s) {
        string S = "";
        for(int i = 0; i= 'a' && s[i] <= 'z') && !(s[i] >= 'A' && s[i] <= 'Z') && !(s[i] >= '0' && s[i] <= '9'))
                continue;
            if(s[i] >= 'A' && s[i] <= 'Z')
                S += (s[i] - 'A' + 'a');
            else
                S += s[i];
        }
        for(int i = 0, j = S.length() - 1; i <= j; i++, j--)
            if(S[i] != S[j])
                return false;
        return true;
    }
};

 

 

 

你可能感兴趣的:(LeetCode,leetcode,算法)