前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Q125 Valid Palindrome

Q125 Valid Palindrome

作者头像
echobingo
发布2018-04-25 16:51:04
5220
发布2018-04-25 16:51:04
举报
文章被收录于专栏:Bingo的深度学习杂货店

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

代码语言:javascript
复制
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.

解题思路:

参考题目 Q9 Palindrome Number

只包含字母或数字的回文数,因此先全部转化为小写,然后过滤掉其他非字母或非数字字符,然后反转比较。

Python实现:
代码语言:javascript
复制
class Solution:
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        s = s.lower() # 先将字符串转化为全小写
        filtStr = ''
        for ch in s:  # 过滤掉非字母和非数字字符
            if 'a' <= ch <= 'z' or '0' <= ch <= '9':
                filtStr += ch
        if filtStr == filtStr[::-1]:
            return True
        else:
            return False

a = 'aBc 122-1c, b:A'
b = Solution()
print(b.isPalindrome(a))  # True
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.02.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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