现在的位置: 首页 > 综合 > 正文

Valid Palindrome

2019年07月25日 ⁄ 综合 ⁄ 共 770字 ⁄ 字号 评论关闭

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:
    char strLwr(char c)
    {
        if(c-'A'>=0 && c-'A'<=26)
        {
            return 'a'+c-'A';
        }   
        return c; 
    }    
    bool isAlphanumeric(char c)
    {
        if(c-'0'>=0 && c-'0'<=9)
        {
            return true;
        }    
        if (c-'a'>=0 && c-'a'<=26)
        {
            return true;
        }    
        if (c-'A'>=0 && c-'A' <= 26)
        {
            return true;
        }    
        return false;
    }     
    bool isPalindrome(string s) {
        int len = s.length();
        string ss = "";
        int i;
        for(i=0; i<len; ++i)
        {
            if(isAlphanumeric(s[i]))
            {
                ss += strLwr(s[i]);
            }    
        }  
        string rss(ss.rbegin(), ss.rend());  
        if (ss == rss)
        {
            return true;
        }    
        return false;
    }
}; 
【上篇】
【下篇】

抱歉!评论已关闭.