Seawolf 发表于 2020-8-3 21:34:56

Leetcode 125. Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:

Input: "race a car"
Output: false


Constraints:

s consists only of printable ASCII characters.

class Solution:
    def isPalindrome(self, s: str) -> bool:
      copy = s[:]
      s = list(s.lower())
      start = 0
      end = len(s) - 1
      while start < end:
            while start < end and not (97 <= ord(s) <= 122 or 48 <= ord(s) <= 57):
                start += 1
            while start < end and not (97 <= ord(s) <= 122 or 48 <= ord(s) <= 57):
                end -= 1
            if s == s:
                start += 1
                end -= 1
            else:
                return False
      return True
页: [1]
查看完整版本: Leetcode 125. Valid Palindrome