鱼C论坛

 找回密码
 立即注册
查看: 3240|回复: 0

[学习笔记] Leetcode 125. Valid Palindrome

[复制链接]
发表于 2020-8-3 21:34:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

  2. Note: For the purpose of this problem, we define empty string as valid palindrome.

  3. Example 1:

  4. Input: "A man, a plan, a canal: Panama"
  5. Output: true
  6. Example 2:

  7. Input: "race a car"
  8. Output: false


  9. Constraints:

  10. s consists only of printable ASCII characters.
复制代码

  1. class Solution:
  2.     def isPalindrome(self, s: str) -> bool:
  3.         copy = s[:]
  4.         s = list(s.lower())
  5.         start = 0
  6.         end = len(s) - 1
  7.         while start < end:
  8.             while start < end and not (97 <= ord(s[start]) <= 122 or 48 <= ord(s[start]) <= 57):
  9.                 start += 1
  10.             while start < end and not (97 <= ord(s[end]) <= 122 or 48 <= ord(s[end]) <= 57):
  11.                 end -= 1
  12.             if s[start] == s[end]:
  13.                 start += 1
  14.                 end -= 1
  15.             else:
  16.                 return False
  17.         return True
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 10:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表