1 Star 0 Fork 0

wd6/LeetCode-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
valid-palindrome-ii.py 999 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 8年前 . Update valid-palindrome-ii.py
# Time: O(n)
# Soace: O(1)
# Given a non-empty string s, you may delete at most one character.
# Judge whether you can make it a palindrome.
#
# Example 1:
# Input: "aba"
# Output: True
# Example 2:
# Input: "abca"
# Output: True
# Explanation: You could delete the character 'c'.
# Note:
# The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
class Solution(object):
def validPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
def validPalindrome(s, left, right):
while left < right:
if s[left] != s[right]:
return False
left, right = left+1, right-1
return True
left, right = 0, len(s)-1
while left < right:
if s[left] != s[right]:
return validPalindrome(s, left, right-1) or validPalindrome(s, left+1, right)
left, right = left+1, right-1
return True
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wd6/LeetCode-1.git
git@gitee.com:wd6/LeetCode-1.git
wd6
LeetCode-1
LeetCode-1
master

搜索帮助