【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 ...
随机推荐
- python_面试题_TCP的三次握手与四次挥手问题
1.相关问题 问题1: 请详细描述三次握手和四次挥手的过程,并画出状态图 问题2: 四次挥手中TIME_WAIT状态存在的目的是什么? 问题3: TCP是通过什么机制保障可靠性的? 2.问题回答 问题 ...
- 【VS开发】动态创建ActiveX控件
bool CCollectDataDlgDlg::CreateMyCtrl(LPRECT lpRect, UINT nID, CWnd *pParent) { CLSID clsid; wstri ...
- 使用graphics.h来绘制图形
| 版权声明:本文为博主原创文章,未经博主允许不得转载. graphics.h是TC里面的图形库,如果要用的话应该用TC来编译.分为:像素函数.直线和线型函数.多边形函数.填充函数等.然而在我们使 ...
- PostgreSQL-pg_ctl
命令简介 pg_ctl 启动.关闭.重启 postgres pg_ctl start [-w] [-s] [-D datadir] [-l filename] [-o options] [-p pat ...
- 从头到尾说一次 Java 垃圾回收,写得非常好! (转)
之前上学的时候有这个一个梗,说在食堂里吃饭,吃完把餐盘端走清理的,是 C++ 程序员,吃完直接就走的,是 Java 程序员.
- 匿名函数lambda和map函数
一.map函数,实现迭代操作 map(f1,x) f1为函数的名称(不加括号),x为map的参数,示例如下: def add(a): return a+10 print map(add,[1,2,3] ...
- Vue2 & ElementUI实现管理后台之input获得焦点
Vue.directive('focus', function (el, option) { var defClass = 'el-input', defTag = 'input'; var valu ...
- ThinkPHP验证器验证规则编码要点
首先验证器要继承框架的think\Validate类. 1.验证规则是一个父类的rule属性,是一个数组. 2.数组的键名是验证字段标识,值是验证规则.多个验证规则要用|分隔,不能有空格,否则可能会验 ...
- SAP发布wbservice,如果有权限管控的话,需要给这个webservice加权限
1. PFCG床架角色 2.在角色菜单上,添加其他,选中Authorization Default Values for Services 如下图 3.选中发布的webservice 后保存,如下图: ...
- 数据库备份及SQL脚本导入
数据库备份及SQL脚本导入 数据导出 su - oracle exp 数据库用户名/数据库密码@ORCL file=20190905.dmp full=y SQL脚本导入 首先导入前查看Oracle用 ...