首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode 125 Valid Palindrome

Leetcode 125 Valid Palindrome

作者头像
triplebee
发布2018-01-12 14:54:40
4900
发布2018-01-12 14:54:40
举报

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 isPalindrome(string s) {
        string temp;
        for(int i=0;i<s.size();i++)
        {
            if((s[i]>='a' && s[i]<='z') || (s[i]>='0' && s[i]<='9')) temp+=s[i];
            if((s[i]>='A' && s[i]<='Z') ) temp+=s[i]-'A'+'a';
        }
        string temp2=temp;
        reverse(temp.begin(),temp.end());
        return temp==temp2;
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-10-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档