LeetCode 125. 验证回文串

简介: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

题目


给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。


示例 1:
输入: "A man, a plan, a canal: Panama"
输出: true
示例 2:
输入: "race a car"
输出: false

解题思路

class Solution:
    def isPalindrome(self, s: str) -> bool:
        #数组
        # sList = []
        # for i in s:
        #     if i.isalnum():#判断是否是字母
        #         sList.append(i.lower())#变成小写并写入
        # midNum = len(sList)//2
        # if sList[:midNum] == sList[::-1][:midNum]:
        #     return True
        # return False
        #双指针
        left = 0
        right = len(s)-1
        while right - left > 0:
            # print(s[left])
            # print(s[right])
            if s[left].isalnum() and s[right].isalnum() and s[left].lower() == s[right].lower():
                left += 1
                right -= 1
            elif s[left].isalnum() == False:
                left += 1
            elif s[right].isalnum() == False:
                right -= 1
            else:
                return False
        return True


目录
相关文章
|
2月前
|
存储 canal 算法
[Java·算法·简单] LeetCode 125. 验证回文串 详细解读
[Java·算法·简单] LeetCode 125. 验证回文串 详细解读
28 0
|
4月前
|
Go
golang力扣leetcode 132.分割回文串II
golang力扣leetcode 132.分割回文串II
27 0
|
6天前
leetcode代码记录(对称二叉树 中序遍历+回文串 为什么不行
leetcode代码记录(对称二叉树 中序遍历+回文串 为什么不行
7 0
|
4月前
|
Go
golang力扣leetcode 98. 验证二叉搜索树
golang力扣leetcode 98. 验证二叉搜索树
16 0
|
4月前
|
存储 算法 C++
leetcode131分割回文串刷题打卡
leetcode131分割回文串刷题打卡
20 0
|
4月前
|
测试技术
leetcode98验证二叉搜索树刷题打卡
leetcode98验证二叉搜索树刷题打卡
18 0
|
4月前
|
Java
leetcode-131:分割回文串
leetcode-131:分割回文串
22 1
|
4月前
|
C++ Python
leetcode-98:验证二叉搜索树
leetcode-98:验证二叉搜索树
34 1
|
4月前
leetcode-125:验证回文串
leetcode-125:验证回文串
25 0
|
5月前
|
算法 vr&ar 图形学
☆打卡算法☆LeetCode 214. 最短回文串 算法解析
☆打卡算法☆LeetCode 214. 最短回文串 算法解析