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. redhat vi 命令

    转载:http://www.cnblogs.com/zhanglong0426/archive/2010/10/07/1845268.html http://blog.sina.com.cn/s/bl ...

  2. Elasticsearch学习之深入搜索五 --- phrase matching搜索技术

    1. 近似匹配 什么是近似匹配,两个句子 java is my favourite programming language, and I also think spark is a very goo ...

  3. LeetCode 49 Group Anagrams(字符串分组)

    题目链接: https://leetcode.com/problems/anagrams/?tab=Description   Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...

  4. ubuntu下文件压缩/解压缩

    ubuntu下文件压缩/解压缩 http://blog.csdn.net/luo86106/article/details/6946255 .gz 解压1:gunzip FileName.gz 解压2 ...

  5. SSH使用秘钥和别名登陆服务器

    手工配置免密码及别名登陆 第一步:生成秘钥 $ ssh-keygen -t rsa 第二步:上传公钥到目标服务器 $ ssh-copy-id -i ~/.ssh/id_rsa.pub <romt ...

  6. 【BZOJ4361】isn 动态规划+树状数组+容斥

    [BZOJ4361]isn Description 给出一个长度为n的序列A(A1,A2...AN).如果序列A不是非降的,你必须从中删去一个数, 这一操作,直到A非降为止.求有多少种不同的操作方案, ...

  7. jmeter聚合报告导出时乱码的解决

    在使用jmeter性能测试时,聚合报告导出后使用excel打开时是乱码,查看相关文件后是编码的问题,解决方法如下: 1.现象: 用excel打开变成这种乱码无法看清 2.解决: 先使用记事本打开后,选 ...

  8. Android ActivityManager与WindowManager

    <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission ...

  9. squid白名单

    http_access deny all #取消注释 http_access allow all --> http_access allow xxx_custom_ip #添加系统服务器IP白名 ...

  10. dataframe转换为多维矩阵,然后可以使用values来实现

    import pandas as pd import numpy as np df = pd.DataFrame(np.random.rand(3,3),columns=list('abc'),ind ...