码迷,mamicode.com
首页 > 其他好文 > 详细

验证回文字符串

时间:2019-04-08 18:48:14      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:can   忽略   使用   lower   def   https   sel   大小写   elf   

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

输入: "A man, a plan, a canal: Panama"
输出: true

 

代码:

思路,这里涉及到了数据清洗,我只要字母和数字,并且字母必须是小写。使用 string,isalnum()可以滤出字母和数字,使用 string.lower()可以滤出小写字母。然后再转换成 list 反转对比即可。

http://www.runoob.com/python/python-strings.html

知识点:

python对空值的判断:

https://www.jianshu.com/p/a0d273550f70

 

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if s is None:
            return Ture

        temp =[ i.lower() for i in s  if i.isalnum()]
        return True if temp==temp[::-1] else False

 

验证回文字符串

标签:can   忽略   使用   lower   def   https   sel   大小写   elf   

原文地址:https://www.cnblogs.com/guangluwutu/p/10672350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!