is_palindrome

  • 2022-12-14
  • 浏览 (580)

is_palindrome.py 源码

# 验证回文串
# https://leetcode-cn.com/problems/valid-palindrome

class Solution:
    def isPalindrome(self, s: str) -> bool:
        if len(s) <= 1:
            return True
        i, j = 0, len(s) - 1
        while i < j:
            if not s[i].isalnum():
                i += 1
                continue
            if not s[j].isalnum():
                j -= 1
                continue
            if s[i].lower() != s[j].lower():
                return False
            i, j = i + 1, j - 1
        return True

你可能感兴趣的文章

first_unique_char

implement_strstr

length_of_ast_word

0  赞