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.

125. Valid Palindrome 的拓展,给定一个非空字符串,最多可删除1个字符,判断是否可以变成回文。

解法:还是从首尾两边开始比较,如果匹配就移动指针继续比较。当遇到不匹配的时候,删除左边的字符或者右边的字符,只要有一种能匹配就继续。

Python:

class Solution(object):
def validPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
def validPalindrome(s, left, right):
while left < right:
if s[left] != s[right]:
return False
left, right = left+1, right-1
return True left, right = 0, len(s)-1
while left < right:
if s[left] != s[right]:
return validPalindrome(s, left, right-1) or validPalindrome(s, left+1, right)
left, right = left+1, right-1
return True

C++:

class Solution {
public:
bool validPalindrome(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) {
return validPalindrome(s, left, right - 1) || validPalindrome(s, left + 1, right);
}
++left, --right;
}
return true;
} private:
bool validPalindrome(const string& s, int left, int right) {
while (left < right) {
if (s[left] != s[right]) {
return false;
}
++left, --right;
}
return true;
}
};

类似题目:

[LeetCode] 125. Valid Palindrome 验证回文字符串

All LeetCode Questions List 题目汇总

[LeetCode] 680. Valid Palindrome II 验证回文字符串 II的更多相关文章

  1. 【LeetCode】680. Valid Palindrome II 验证回文字符串 Ⅱ(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 思路来源 初版方案 进阶方案 日期 题目地址 ...

  2. LeetCode OJ:Valid Palindrome(验证回文)

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  3. Leetcode680.Valid Palindrome II验证回文字符串2

    给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca" 输出: ...

  4. 力扣(LeetCode)验证回文字符串II 个人题解

    给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca" 输出: ...

  5. [LeetCode] Valid Palindrome II 验证回文字符串之二

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

  6. 【LeetCode】- Valid Palindrome(右回文)

    [ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...

  7. LeetCode 125 Valid Palindrome(有效回文)(*)

    翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...

  8. 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 ...

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

    680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...

随机推荐

  1. CentOS7安装Pycharm

    1. 进入官网:https://www.jetbrains.com/pycharm/2. 点击下载3. 直接安装:tar zxvf ***.tar.gz4. 建立软连接:sudo ln -s /you ...

  2. 《逆袭团队》第九次团队作业【Beta】Scrum meeting 3

    项目 内容 软件工程 任课教师博客主页链接 作业链接地址 团队作业9:Beta冲刺与团队项目验收 团队名称 逆袭团队 具体目标 (1)掌握软件黑盒测试技术:(2)学会编制软件项目总结PPT.项目验收报 ...

  3. Hadoop跨集群迁移数据(整理版)

    1. 什么是DistCp DistCp(分布式拷贝)是用于大规模集群内部和集群之间拷贝的工具.它使用Map/Reduce实现文件分发,错误处理和恢复,以及报告生成.它把文件和目录的列表作为map任务的 ...

  4. git基本操作及设置

    $ git config --global user.name "wstmljf" $ git config --global user.email "wstmljf@1 ...

  5. 【学习笔记】Kruskal 重构树

    1. 例题引入:BZOJ3551 用一道例题引入:BZOJ3551 题目大意:有 \(N\) 座山峰,每座山峰有他的高度 \(h_i\).有些山峰之间有双向道路相连,共 \(M\) 条路径,每条路径有 ...

  6. Django API接口FK ManyTo Many 模板

    Url from django.contrib import admin from django.urls import path, re_path from django.urls import i ...

  7. LeetCode 875. Koko Eating Bananas

    原题链接在这里:https://leetcode.com/problems/koko-eating-bananas/ 题目: Koko loves to eat bananas.  There are ...

  8. wget递归下载网站资源

    wget -r -p -np -k http://archive.openwrt.org/barrier_breaker/14.07/ramips/mt7620a/packages/ 在下载https ...

  9. php Web 项目的文件/文件夹上传下载

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...

  10. 钟长者P71解题报告

    T1 [题目描述] 给你N个字符串,你每次可以选择其中一个字符串的一段前缀进行翻转,但是你必须保证这个前缀的长度是偶数.你可以进行无限次这样的操作,并且如果两个字符串变得相同的时候,你就可以把这两个字 ...