【LeetCode】125. Valid Palindrome 解题报告(Python)

127 篇文章 2 订阅

题目大意:
验证回文字符串,忽略大小写,空格
在这里插入图片描述

解题思路:
1.全转化为小写
2.左右指针标记,左要小于右,进入循环
3.非字母数字直接移动指针
4.左右相同,同时移动指针
5.左右不同直接返回错误

提交代码:(Runtime: 48 ms, faster than 69.48 % )

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = s.lower()
        left, right = 0, len(s)-1
        while left < right:
            a = s[left]
            b = s[right]
            if not ((s[left] >= 'a' and s[left] <= 'z') or (s[left] >= '0' and s[left] <= '9')):
                left += 1
                continue
            if not ((s[right] >= 'a' and s[right] <= 'z') or (s[right] >= '0' and s[right] <= '9')):
                right -= 1
                continue
            if s[left] == s[right]:
                left += 1
                right -= 1
            else:
                return False
        return True

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值