Skip to content

Latest commit

 

History

History
46 lines (32 loc) · 853 Bytes

125._valid_palindrome.md

File metadata and controls

46 lines (32 loc) · 853 Bytes

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]