python实现leetcode之125. 验证回文串

2021-10-06  本文已影响0人  深圳都这么冷

解题思路

双指针,一个从左往右,另一个从右往左
跳过非字母的字符,对碰到的字符,转化为小写然后比较

125. 验证回文串

代码

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        # s = [c.lower() for c in s if c.isalnum()]
        length = len(s)
        low, high = 0, length-1
        while low < high:
            while not s[low].isalnum() and low < high:
                low += 1
            while not s[high].isalnum() and low < high:
                high -= 1
            if s[low].lower() != s[high].lower():
                return False
            low += 1
            high -= 1
        return True
效果图
上一篇 下一篇

猜你喜欢

热点阅读