鱼C论坛

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

[学习笔记] Leetcode 680. Valid Palindrome II

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

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

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

x
  1. Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

  2. Example 1:
  3. Input: "aba"
  4. Output: True
  5. Example 2:
  6. Input: "abca"
  7. Output: True
  8. Explanation: You could delete the character 'c'.
  9. Note:
  10. The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
复制代码

  1. class Solution:
  2.     def validPalindrome(self, s: str) -> bool:
  3.         start = 0
  4.         end = len(s) - 1
  5.         
  6.         while start <= end:
  7.             if s[start] != s[end]:
  8.                 first = s[start: end]
  9.                 second = s[start + 1: end + 1]
  10.                 return first == first[::-1] or second == second[::-1]
  11.             start += 1
  12.             end -= 1
  13.             
  14.         return True
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-4 03:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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