【leetcode】Valid Palindrome II
很久没有做题了,今天写个简单难度的练练手感。
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.
分析:这个题目的要求是判断回文,但是增加了一点难度,可以删掉至多一个字符。所以,我们可以从字符串的两头往中间进行每个字符的比较。先比较s[0]和s[len(s)-1],如果相同的话,低位+1,高位-1,直到找到两个字符不同为止。因为可以删除一个字符,所以还需要判断s[低位+1] == s[高位] 和 s[低位] == s[高位-1],如果满足其中一个条件,那么对应位置+1或者-1继续比较。如果两个都满足,我的方法是先比较s[低位] == s[高位-1],如果不是回文,再来比较s[低位+1] == s[高位]。
代码:
class Solution(object):
def validPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
low = 0;
high = len(s)-1
isDel = False
fisrtFlag = false
firstLow = 0
firstHigh = 0
ret = True
while low < high:
if s[low] == s[high]:
low += 1
high -= 1
continue
else:
if isDel == True:
ret = False
break
if s[low] != s[high-1] and s[low+1] != s[high]:
ret = False
break
elif s[low] == s[high-1]:
firstLow = low
firstHigh = high-1
fisrtFlag = True
high -= 1
isDel = True
elif s[low+1] == s[high]:
low += 1
isDel = True
#再比较异常
if ret == False and fisrtFlag == True:
ret = True
low = firstLow + 1
high = firstHigh + 1
while low < high:
if s[low] == s[high]:
low += 1
high -= 1
continue
else:
ret = False
break
return ret
【leetcode】Valid Palindrome II的更多相关文章
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【LeetCode】- Valid Palindrome(右回文)
[ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【Leetcode】【Easy】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
随机推荐
- 应用安全 - 平台 | 工具 - Centreon Web - 漏洞 - 汇总
简介 产地 法国 用途 监控 分布 CVE-2019-16405 https://thecybergeek.co.uk/cves/2019/09/19/CVEs.html
- 2019.8中关村、OGeek(oppo)比赛
中关村writeup https://mp.weixin.qq.com/s?__biz=MzU3MzczNDg1OQ==&mid=2247484106&idx=1&sn=62a ...
- Python的入门(day1)
一:Python的起源 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC 语言的一种 ...
- sql中循环的存储过程
), a2 bigint, a3 bigint) returns void as $$declare ii integer; begin II:; FOR ii IN a2..a3 LOOP INSE ...
- Boruvka
大概是这样的:一开始图中有\(n\)个连通块,每次操作我们选出各个连通块连出去的最短的边(如果有相同边权的边的话可以把序号作为第二关键字),然后把这些边加入最小生成树. 最坏的情况下每次操作都会让当前 ...
- gym102201F_Fruit Tree
题意 给一棵带权树,多次询问路径上出现次数超过一半的数. 分析 dfs序建主席树,维护的就是根到某个节点这段路径的值域情况. 因为题目所求的不是一般的众数,而是出现次数大于一半的,所以在主席树上可以直 ...
- CSA Lignts Out
csa 算是热身题吧 如果是每次操作一行或一列,那么无论怎么操作,本质不同的行最多只有两种,本质不同的列也最多只有两种,那么只要把某一种行和某一种列全部翻转使得全为0即可 现在是同时操作一行一列,显然 ...
- vue-cli设置引入目录
打开build/webpack.base.conf.js 找到module.exports下的resolve这行 刚开始是这样的 resolve: { extensions: ['.js', '.vu ...
- spring boot引入thymeleaf导致中文乱码
加上下面这句代码 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" / ...
- vue history模式 ios微信分享坑
vue history模式 ios微信分享坑 问题分析:因为苹果分享会是调取签名失败是因为:苹果在微信中浏览器机制和安卓不同,有IOS缓存问题,和IOS对单页面的优化问题,通俗点说安卓进行页面跳转分享 ...