[TOC]
题目
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
1输入: "A man, a plan, a canal: Panama" 2输出: true
示例 2:
1输入: "race a car" 2输出: false
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/valid-palindrome 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答
思路:
先根据ascii编码,将原本字符中的不需要的字符和大写的字母去掉或者替换掉,然后前后进行对比
leetcode解题
1class Solution: 2 def isPalindrome(self, s: str) -> bool: 3 s_x="" 4 for i in s: 5 ord_i = ord(i) 6 if 97<=ord_i<=122 or 48<=ord_i<=57: 7 s_x+=i 8 elif 65<=ord_i<=90: 9 s_x+=chr(ord_i+32) 10 for i in range(len(s_x)): 11 if s_x[i]!=s_x[-(i+1)]: 12 return False 13 return True