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 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.
要完成的函数:
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的更多相关文章
- [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]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 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 ...
- LeetCode 680. Valid Palindrome II(双指针)
题意:给定一个字符串,可以最多去掉一个字符,判断是否可以使该字符串成为一个回文串. 分析:head和tail双指针分别指向字符串首尾,如果s[head] != s[tail],则要么去掉s[head] ...
- 680. Valid Palindrome II【easy】
680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...
- 【Leetcode_easy】680. Valid Palindrome II
problem 680. Valid Palindrome II solution: 不理解判断函数中的节点的问题... class Solution { public: bool validPali ...
- 【LeetCode】680. Valid Palindrome II
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...
- 【LeetCode】680. Valid Palindrome II 验证回文字符串 Ⅱ(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 思路来源 初版方案 进阶方案 日期 题目地址 ...
- Python 解LeetCode:680. Valid Palindrome II
题目:给定一个字符串,在最多删除一个字符的情况下,判断这个字符串是不是回文字符串. 思路:回文字符串,第一想到的就是使用两个指针,前后各一个,当遇到前后字符不一致的时候,有两种情况,删除前面字符或者删 ...
- [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 ...
随机推荐
- Opencv Laplace算子
//通过拉普拉斯-锐化边缘 kernel = (Mat_<float>(3,3)<<1,1,1,1,-8,1,1,1,1);//Laplace算子 filter2D(img2, ...
- Python的内建比较函数cmp比较原理剖析-乾颐堂
cmp( x, y):比较2个对象,前者小于后者返回-1,相等则返回0,大于后者返回1. Python的cmp比较函数比较原理 Python的cmp函数可以比较同类型之间,或者不同数据类型之间.然后根 ...
- mysqli_query(“set nams utf8”)为FALSE
经过过测试: mysqli_query("set nams utf8")必须在所有语句的执行之前,否则会返回FALSE. 看手册,说不建议用mysqli_query来设置编码,用 ...
- sql分组获取第一条记录(sql+oracle)
sql版本 select * from (select t.CloseDate,t.ExpiryDate,t.DataTypeLookupID,ROW_NUMBER() over(partition ...
- BBS项目之后台管理
一:后台管理,添加文章样式编写 创建 一个后台管理模板前段页面 <!DOCTYPE html> <html lang="en"> <head> ...
- Selenium模拟浏览器初识
Seleniumd介绍 在写Python爬虫的时候,最麻烦的不是那些海量的静态网站,而是那些通过JavaScript获取数据的站点.Python本身对js的支持不好,所以就有良心的开发者来做贡献了,这 ...
- es学习-索引别名
别名不能重复,也不能喝索引名称重复.(一个索引可以创建多个别名) 语法: 添加一个别名: url:POST http://192.168.0.108:9200/_aliases/ 参数: { &quo ...
- ThinkPhp 生成静态页面
//开启静态缓存'HTML_CACHE_ON' => true, //开启缓存'HTML_CACHE_TIME' =>60, //开启缓存时间'HTML_FILE_SUFFIX' => ...
- ping别的电脑出错
原因ifconfig 电脑1:172.31.45.101 电脑2:172.31.188.232 http://ask.csdn.net/questions/178358 如何防止别人ping自己的电脑 ...
- Unity NetWork
using UnityEngine; using System.Collections; public class NetworkTest : MonoBehaviour { ;//端口号 strin ...