题目描述:

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.

要完成的函数:

bool validPalindrome(string s)

说明:

1、给定一个字符串,至多删去里面的一个字符,使其成为一个回文串。判断能不能实现。

2、我们先看一个例子

abca

acba(反转)

我们可以看到第一个字母是一样的,第二个字母的时候就对不上了,那我们如果要形成一个回文串,可以试着删去第二个字母b,也可以删去从后面数起第二个字母c,只要之后能一一对上,那么还是一个回文串。

关于这两种情况,给两个例子来说明一下:

ececabbacec,这个字符串,我们看到第一个字符e和最后一个字符c没对上,我们可以删去e,这样子能形成回文串。我们也能删去c,但这样形成不了回文串。这是要删去前面字符的例子。

abbca,这个字符串,如果删去前面字符b,那么形成不了回文串,如果删去后面字符c,刚好能形成回文串。这是要删去后面字符的例子。

所以我们要分成两种情况来判断处理。

按照上述逻辑来处理一下,构造如下代码:(附解释)

    bool validPalindrome(string s)
{
int i=,j=s.size()-;
while(i<j)
{
if(s[i]!=s[j])//当碰到对不上的时候,记录i和j的值
break;
i++;
j--;
}
int i1=i,j1=j;//记录一个副本
i++;//如果删去前面的字符,i++,处理下一个
bool flag=;
while(i<j)
{
if(s[i]!=s[j])//如果出现第二次对不上的情况
{
flag=;
break;
}
i++;
j--;
}
if(flag==)//如果全程都对得上,那么返回true
return true;
j1--;//如果删去前面的字符对不上了,那么删去后面的字符试试
while(i1<j1)
{
if(s[i1]!=s[j1])//如果还是对不上,返回false
return false;
i1++;
j1--;
}
return true;//如果全程对得上,那么返回true
}

上述代码实测119ms,beats 90.33% of cpp submissions。

leetcode-680-Valid Palindrome II的更多相关文章

  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】680. Valid Palindrome II

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...

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

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

  9. Python 解LeetCode:680. Valid Palindrome II

    题目:给定一个字符串,在最多删除一个字符的情况下,判断这个字符串是不是回文字符串. 思路:回文字符串,第一想到的就是使用两个指针,前后各一个,当遇到前后字符不一致的时候,有两种情况,删除前面字符或者删 ...

  10. [LeetCode] 680. Valid Palindrome II_Easy tag: Two Pointers

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

随机推荐

  1. c++ 命令模式(command)

    命令模式的有点: 1.能够容易地设计一个命令队列: 2.在需要的情况下,可以比较容易地将命令记入日志. 3.可以容易的实现对请求的撤销和重做. 4.由于加进新的具体命令类不影响其他的类,因此增加新的具 ...

  2. Tomcat安装 以Linux 分支 Ubuntu Server 为例

    以Linux 分支 Ubuntu Server 为例.一.相关目录及作用说明 /etc/tomcat6 - 全局配置 /usr/share/tomcat6/ - 程序主目录 /usr/share/to ...

  3. 66. Plus One 数组加1

    [抄题]: Given a non-negative integer represented as a non-empty array of digits, plus one to the integ ...

  4. python准确判断文件类型

    判断文件类型在开发中非常常见的需求,怎样才能准确的判断文件类型呢?首先大家想到的是文件的后缀,但是非常遗憾的是这种方法是非常不靠谱的,因为文件的后缀是可以随意更改的,而大家都知道后缀在linux系统下 ...

  5. c linux ping 实现

    摘自:https://blog.csdn.net/weibo1230123/article/details/79891018 ping的实现和代码分析一.介绍     ping命令是用来查看网络上另一 ...

  6. Hive入门学习随笔(二)

    ====使用Load语句执行数据的导入 --将操作系统上的文件student01.txt数据导入到t2表中 load data local inpath '/root/data/student01.t ...

  7. hdu Lovekey(水题)

    #include<stdio.h> #include<string.h> ]; int main() { ],s2[]; int l1,l2,i,j,k; while(scan ...

  8. 为 pycharm 修改 Theme & Color

    版本: pycharm-community-4.5.2 安装之后先导入 (File --> Import Setting --> django.jar) 将文件pycharm-themes ...

  9. 设计模式18:Observer 观察者模式(行为型模式)

    Observer 观察者模式(行为型模式) 动机(Motivation) 在软件构建过程中,我们需要为某些对象建立一种“通知依赖关系”——一个对象(目标对象)的状态发生改变,所有依赖对象(观察者对象) ...

  10. 编写高质量代码改善C#程序的157个建议——建议99:重写时不应使用子类参数

    建议99:重写时不应使用子类参数 重写时,如果使用了子类参数,可能会偏离设计者的预期目标.比如,存在一个如下继承体系: class Employee { } class Manager : Emplo ...