Skip to content

125 valid palindrome

125. Valid Palindrome

题目: https://leetcode.com/problems/valid-palindrome/

难度:

Easy

就是比较reversed string 和原本的是否相等.

class Solution(object):
    def isPalindrome(self,s):
        """
        :type s: str
        :rtype: bool
        """

        new=[]  
        s = s.lower()  

        for i in s:  
            if '0'<=i<='9' or 'a'<=i<='z':  
                new.append(i)  

        return new == new[::-1]  

或者用正则,详见re.sub()用法 瞬间beats 97.71%

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        newString = re.sub("[^0-9a-zA-Z]+", "", s)
        return newString.lower() == newString.lower()[::-1]


回到顶部