验证回文串(预处理)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36372879/article/details/88066684

有空格,大写和小写,可以使用预处理的方法
在这里插入图片描述

class Solution:
    def isPalindrome(self, s: str) -> bool:
        if s == '':
            return True
        s = [c.lower() for c in s if c.isdigit() or c.isalpha()]
        i, j = 0, len(s) - 1
        while i < j:
            if s[i] != s[j]:
                return False
            i, j = i + 1, j - 1
        return True

猜你喜欢

转载自blog.csdn.net/weixin_36372879/article/details/88066684