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

125. Valid Palindrome(判断忽略标点的字符串是否回文,加个正则,与上一题解法一样)

时间:2018-02-18 16:20:39      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:字符   return   char   turn   only   回文   color   eric   ons   

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.

 

 1 class Solution:
 2     def isPalindrome(self, s):
 3         """
 4         :type s: str
 5         :rtype: bool
 6         """
 7         if s==‘‘:
 8             return True
 9         import re
10         x = re.sub(\W,‘‘,s).lower()
11         if x==‘‘:
12             return True
13         if(len(x)%2!=0):#
14             for i in range(int(len(x)/2)+1):
15                 if(x[i]!=x[len(x)-i-1]):
16                     return False
17         else:
18             for i in range(int(len(x)/2)+1):
19                 if(x[i]!=x[len(x)-i-1]):
20                     return False
21         return True
22         

 

125. Valid Palindrome(判断忽略标点的字符串是否回文,加个正则,与上一题解法一样)

标签:字符   return   char   turn   only   回文   color   eric   ons   

原文地址:https://www.cnblogs.com/zle1992/p/8452840.html

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