2 Star 0 Fork 0

ShiboJiang / per_leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
125.验证回文串.py 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
姜世博 提交于 2021-10-12 22:54 . update 验证回文串
#
# @lc app=leetcode.cn id=125 lang=python3
#
# [125] 验证回文串
#
# @lc code=start
class Solution:
def isPalindrome(self, s: str) -> bool:
tp_rtn = True
first_ptr = 0
last_ptr = len(s) - 1
while (first_ptr < last_ptr):
frt_str = s[first_ptr]
lst_str = s[last_ptr]
# filter none ascii str
while not frt_str.isalnum():
first_ptr = first_ptr + 1
frt_str = s[first_ptr]
# break loop
if first_ptr >= last_ptr:
break
while not lst_str.isalnum():
last_ptr = last_ptr - 1
lst_str = s[last_ptr]
# break loop
if first_ptr >= last_ptr:
break
# Judge whether first str equal last str
if (frt_str.isalnum() and lst_str.isalnum()):
if frt_str.lower() != lst_str.lower():
# Not valid str
tp_rtn = False
break
first_ptr = first_ptr + 1
last_ptr = last_ptr - 1
# Return result
return tp_rtn
# @lc code=end
test_str = '0P'
tp_func = Solution()
print(tp_func.isPalindrome(test_str))
1
https://gitee.com/qq353838430/per_leetcode.git
git@gitee.com:qq353838430/per_leetcode.git
qq353838430
per_leetcode
per_leetcode
master

搜索帮助