4

【leetcode】125.验证回文串

 2 years ago
source link: https://blog.feelyou.top/posts/leetcode125.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【leetcode】125.验证回文串

2020-03-04

16

链接:https://leetcode-cn.com/problems/valid-palindrome

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

说明:本题中,我们将空字符串定义为有效的回文串。

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

输入: “race a car”
输出: false

python3

class Solution:
def isPalindrome(self, s: str) -> bool:
s = [*filter(str.isalnum, s.lower())]
return s == s[::-1]

学到两个很牛逼方法:
filter: 过滤方法

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def is_odd(n):
return n % 2 == 1

newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(newlist)

*: 解包

[*range(5)]
[i for i in range(5)]
[0, 1, 2, 3, 4]

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK