leetcode--Palindrome Partitioning

1.题目描述

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.

2.解题思路

题目很简单,但是就是要注意考虑到大小写和空字符串的问题(空字符串包括删除无用字符串之后为空的情况)

class Solution {
public:
    bool isPalindrome(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        if(s.empty())return true;
        
        string s_argument;
        
        s_argument.push_back('#');
        
        for(int i=0;i<s.length();++i)
        {
            if((s[i]<='z'&&s[i]>='a')||(s[i]>='0'&&s[i]<='9')||(s[i]>='A'&&s[i]<='Z'))
            {
                if(s[i]>='A'&&s[i]<='Z')
                {
                    s_argument.push_back(s[i]-'A'+'a');
                }
                else s_argument.push_back(s[i]);
                s_argument.push_back('#');
            }
        }
        
        if(s_argument.length()==1)return true;
        for(int i=s_argument.length()/2-1;i>=0;i--)
        {
            if(s_argument[i]!=s_argument[s_argument.length()-1-i])return false;
            
        }
        
        return true;    
        
    }
};
posted @ 2013-08-09 00:01  曾见绝美的阳光  阅读(191)  评论(0编辑  收藏  举报