一起刷leetcode(125): Valid Palindrome


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.

Link: https://oj.leetcode.com/problems/valid-palindrome/

Related Problems:
9. Palindrome Number
131. Palindrome Partitioning
132. Palindrome Partitioning II
已邀请:

sumnous - 数据挖掘女博士

赞同来自: tmpbook LostOsiris


思路:忽略掉非字母数字字符,然后贴一个超简单的Python版本(That is why I think Python is so hot```)
Python代码(已AC):
class Solution:
    # @param s, a string
    # @return a boolean
    def isPalindrome(self, s):
        newS = []
        for i in s:
            if '0' <= i <= '9' or 'a' <= i <= 'z': newS.append(i)
            elif 'A' <= i <= 'Z': newS.append(chr(ord(i) - ord('A') + ord('a')))
        return newS == newS[::-1] #字符串 与 反转字符串 比较是否相等,相等可证明是回文


要回复问题请先登录注册