LeetCode - Valid Palindrome II
程序员文章站
2024-03-06 23:32:26
...
11.08.2021
小白一枚,文章仅当日记本记录学习进度 ;)
如被浏览,请多多指教,非常感谢大家纠错与建议!
(因留学顺便练习英语,所以部分用英文笔记,并无他意)
Solution - Two pointers (Python)
class Solution:
def validPalindrome(self, s: str) -> bool:
left = 0
right = len(s) - 1
while left < right:
if s[left] != s[right]:
case_1 = s[left+1:right+1]
case_2 = s[left:right]
return case_1 == case_1[::-1] or case_2 == case_2[::-1]
left += 1
right -= 1
return True
Runtime: 100 ms, faster than 81.04% of Python3 online submissions for Valid Palindrome II.
Memory Usage: 14.7 MB, less than 39.51% of Python3 online submissions for Valid Palindrome II.
上一篇: 使用Java编写GUI对话框的教程
下一篇: Java将一个正整数分解质因数的代码