125. Valid Palindrome(判断忽略标点的字符串是否回文,加个正则,与上一题解法一样)
2018-02-18 15:53
295 查看
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
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.
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
相关文章推荐
- C语言:判断一个字符串是否为回问字符串(回文字符串也是一个字符串,但是原字符串和字符串逆序之后一样,比如”abcba","cddc"就是回文字符串
- 判断一个字符串是否回文
- 用正则判断一个字符串是否包含中文
- 第十五周 判断字符串是否为回文
- 正则判断字符串中是否有重复记录
- acm--判断是否回文字符串
- Problem C: 判断字符串是否为回文
- js正则表达式判断一个字符串是否是正确的有数字和小数点组成的金钱形式和 判读数值类型的正则表达式
- 判断两个由大小写字母和空格组成的字符串在忽略大小写和压缩掉空格后是否相等?--POJ--字符串处理
- Java判断字符串是否为数字(正则表达式)
- java用正则表达式判断字符串中是否仅包含英文字母、数字和汉字
- 正则表达式RegularExpression(从字符串中获取想要的部分,判断给定的字符串是否符合正则表达式的过滤逻辑)
- 判断字符串是否为回文
- 用正则表达式判断一个字符串是否全是数字
- C# 判断一字符串是否为合法数字(正则表达式)
- 判断一个字符串是否为回文的非递归算法
- 使用bool函数判断一个字符串是否为回文字符串
- 判断一个字符串String是否是由数字组成,采用正则表达式
- 强化练习6:判断一字符串是否为回文,是返回1,不是返回0,出错返回-1
- 用正则表达式判断字符串是否是数字