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:

  1. The string will only contain lowercase characters a-z. The maximum length of the string is 50000.

题目思路就是正常的用two pointers去遍历s, 然后如果有不一样的, 要么把l + = 1, 要么把r -= 1 , 然后如果这两个中有一个是palindrome, 那么就符合.

Code:

class Solution:
def palindrome2(self, s): # len(s) >= 1
l, r = 0, len(s)-1
def helper(l, r, s):
while l < r:
if s[l] == s[r]:
l += 1
r -= 1
else:
return False
return True
while l <r:
if s[l] == s[r]:
l += 1
r -= 1
else:
return helper(l+1, r, s) or helper(l, r-1, s)
return True

[LeetCode] 680. Valid Palindrome II_Easy tag: Two Pointers的更多相关文章

  1. [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 ...

  2. [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 ...

  3. LeetCode 680. Valid Palindrome II (验证回文字符串 Ⅱ)

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

  4. LeetCode 680. Valid Palindrome II(双指针)

    题意:给定一个字符串,可以最多去掉一个字符,判断是否可以使该字符串成为一个回文串. 分析:head和tail双指针分别指向字符串首尾,如果s[head] != s[tail],则要么去掉s[head] ...

  5. 680. Valid Palindrome II【easy】

    680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...

  6. 【Leetcode_easy】680. Valid Palindrome II

    problem 680. Valid Palindrome II solution: 不理解判断函数中的节点的问题... class Solution { public: bool validPali ...

  7. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. LeetCode 125. Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

  9. [leetcode] 1. Valid Palindrome

    leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...

随机推荐

  1. 游戏服务器学习笔记 5———— twisted Perspective Broker 透明代理

    实际上这章压根不需要我来说,twisted官网的Doc里面有专门介绍的章节.写的非常详细. http://twistedmatrix.com/documents/current/core/howto/ ...

  2. Makefile 中all 和.PHONY的作用

    请编写一个makefile同时编译.链接下面两个程序: main1.c: #include<stdio.h> int main(void) { printf("main1\n&q ...

  3. 部署OpenStack问题汇总(五)--openstack中删除虚拟主机,状态一直未deleting

    [原创文章,转载请注明出处] 一.我重启了该机器,之后想删除没有创建成功的虚拟机(没有打开cpu的vt),结果发现状态一直为deleting状态.在这个状态下创建虚拟机也失败. 二.分析:在/var/ ...

  4. mysql explain分析

    通过explain可以知道mysql是如何处理语句,分析出查询或是表结构的性能瓶颈.通过expalin可以得到: 1. 表的读取顺序 2.表的读取操作的操作类型 3.哪些索引可以使用 4. 哪些索引被 ...

  5. 使用mimikatz获取和创建Windows凭据的工具和方法

    Mimikatz 下载地址 https://github.com/gentilkiwi/mimikatz/releases 本地凭据破解 以管理员身份运行(拿到shell提权后) mimikatz#p ...

  6. RabbitMQ安装详解(centos6.8)(转自:http://www.cnblogs.com/zhen-rh/p/6862350.html)

    1.下载rabbitmq安装包 2.安装erlang a.安装Erlang Solutions仓库到你的系统(目的在于让你可以使用yum安装到最新版本的erlang, 如果不设置, yum安装的erl ...

  7. vue--自定义指令进行验证(1)

    实例代码: <template> <div id="app" class="app"> <h3>{{msg}}</h3 ...

  8. jqGrid 中的editrules来自定义colModel验证规则

    editrules    editrules是用来设置一些可用于可编辑列的colModel的额外属性的.大多数的时候是用来在提交到服务器之前验证用户的输入合法性的.比如editrules:{edith ...

  9. 扩展Spring切面

    概述 Spring的切面(Spring动态代理)在Spring中应用十分广泛,例如还有事务管理,重试等等.网上介绍SpringAop源码很多,这里假设你对SpringAop有基本的了解.如果你认为Sp ...

  10. C#调用C++ DLL的方式

    动态链接库(DLL)是一个包含可由多个程序同时使用的代码和数据的库,DLL不是可执行文件.可以说在windows操作系统中随处可见,打开主分区盘下的system32.在一些项目中,如果有大量运算或者涉 ...