【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
- 原生js实现选中所有的checkbox
<div class="con"> <input name='多选项名称' type='checkbox' value='' id="all" ...
- cocos creator 3D | 拇指投篮 | 3D项目入门实战
你的命中率是多少呢?文章底部试玩! 效果预览 配置环境: Cocos Creator 3D v1.0.1 玩法说明: 触摸屏幕,向上滑动投篮!注意篮板是会移动的哦!看看你的命中率是多少! 实现原理 为 ...
- [转帖]预警 | Linux 爆“SACK Panic”远程DoS漏洞,大量主机受影响
预警 | Linux 爆“SACK Panic”远程DoS漏洞,大量主机受影响 https://cloud.tencent.com/developer/article/1447879 所有的 版本 ...
- String.equals()方法、整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()
equals 是比较的两个字符串是否一样 length() 返回字符串的长度 charAt (int index) 返回index所指定的字符 getChars(int srcBegin,int sr ...
- Kick Start 2019 Round D
X or What? 符号约定: $\xor$ 表示异或. popcount($x$) 表示非负整数 $x$ 的二进制表示里数字 1 出现的次数.例如,$13 = 1101_2$,则 popcount ...
- MacOS 下文件读取问题
使用Xcode编写C++程序可以直接使用fstream读写文件,代码如下: const char* path1 = [path UTF8String];string filename = path1; ...
- 为什么只有ip地址和端口号需要主机字节序到网络字节序的转换?
答复是:因为内容是二进制流,不是整数. 整数(int.uint16.uint32)的表达,是需要多字节的,在不同cpu上,字节次序是不同的.因此,从A主机到B主机,如果是异构的,就需要做字节调整.同构 ...
- x系统清理/tmp/文件夹的原理
转自:http://www.opsers.org/base/clean-up-on-the-linux-system-tmp-folder-you-may-want-to-know.html§ 我们知 ...
- python 安装 colorama 控制台输出彩色文字
pip install colorama from colorama import Back,Fore,Style # 字体颜色print(Fore.LIGHTBLUE_EX,'HelloWorLd' ...