【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 ...
随机推荐
- [IJCAI-17 口碑商家客流量预测]
IJCAI-17 口碑商家客流量预测 第 1 赛季截止日期 2017/03/14 赛制介绍 重要时间2月8日 08:00: 评测启动3月7日 10:00: 报 ...
- 访问DataGridView的Rows报了OutOfIndexRangeException错误
DataGridView绑定了一个List<Entity>实体集合,在遍历DataGridView的每一行DataBoundItem时候,如果符合某个条件的话,则移除List<Ent ...
- ${pagecontext.request.contextpath}绝对路径理解
${pageContext.request.contextPath}是JSP取得绝对路径的方法,等价于<%=request.getContextPath()%> .也就是取出部署的应用程序 ...
- Spring Boot常用功能
1.Spring Boot打war包配置 利用IDEA将SpringBoot的项目打包成war文件
- 开发维护中遇到问题---eclipse、发版问题
1.jar包冲突问题, [服务器启动service服务器,tomcat也已启动]然后浏览器访问,会出现这样子的问题现象:tomcat什么的启动成功,但是访问时会出现404: 解决方法:先停掉tomca ...
- redis内存满了怎么办?
redis最为缓存数据库,一般用于存储缓存数据,用于缓解数据库压力,但是缓存太多,内存满了怎么办呢.一般有以下几种方法 一.增加内存 redis存储于内存中,数据太多,占用太多内存,那么增加内存就是最 ...
- Java作业 题目:16版.情人节送玫瑰花
题目:16版.情人节送玫瑰花 题干: 1.实验要求 本实验要求:以情人节送花为业务背景,体验自定义异常以及异常处理机制. 1-1. 业务说明: 1-1.1. 本实验以情人节送花为业务背景,女方提出送花 ...
- py3 base64加密
import base64 #字符串编码: encodestr = base64.b64encode('abcr34r344r'.encode('utf-8')) print(str(encodest ...
- hadoop批量命令脚本xrsync.sh传输脚本
1.xrsync.sh脚本 #!/bin/bash if [[ $# -lt 1 ]] ; then echo no params ; exit ; fi p=$1 #echo p=$p dir=`d ...
- CentOS 7 安装MongoDB 4.0
1.创建yum源文件 /etc/yum.repos.d/mongodb-org-4.0.repo [mongodb-org-4.0] name=MongoDB Repository baseurl=h ...