-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_5.py
More file actions
43 lines (35 loc) · 895 Bytes
/
question_5.py
File metadata and controls
43 lines (35 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/5/4 9:53
# @Author : cancan
# @File : question_5.py
# @Function : 验证回文字符串
"""
Question:
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
Note:
本题中,我们将空字符串定义为有效的回文串。
Example 1:
输入: "A man, a plan, a canal: Panama"
输出: true
Example 2:
输入: "race a car"
输出: false
"""
class Solution1:
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
import re
s = ''.join(re.findall(r'(\w+|\d+)', s)).lower()
return s == s[::-1]
class Solution2:
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
s = list(filter(str.isalnum, s.lower()))
return s == s[::-1]