LeetCode 680. Valid Palindrome II(双指针)
题意:给定一个字符串,可以最多去掉一个字符,判断是否可以使该字符串成为一个回文串。
分析:head和tail双指针分别指向字符串首尾,如果s[head] != s[tail],则要么去掉s[head],要么去掉s[tail],只需判断s[head + 1]~s[tail]或s[head]~s[tail-1]是否为回文串即可。
class Solution {
public:
bool judge(string s, int head, int tail){
while(head <= tail){
if(s[head] == s[tail]){
++head;
--tail;
}
else return false;
}
return true;
}
bool validPalindrome(string s) {
if(s == "") return true;
int head = 0;
int tail = s.size() - 1;
while(head <= tail){
if(s[head] == s[tail]){
++head;
--tail;
}
else{
return judge(s, head + 1, tail) || judge(s, head, tail - 1);
}
}
return true;
}
};
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 ...
- 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 验证回文字符串 Ⅱ(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 思路来源 初版方案 进阶方案 日期 题目地址 ...
- 【LeetCode】680. Valid Palindrome II
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...
- 680. Valid Palindrome II【Easy】【双指针-可以删除一个字符,判断是否能构成回文字符串】
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- Python 解LeetCode:680. Valid Palindrome II
题目:给定一个字符串,在最多删除一个字符的情况下,判断这个字符串是不是回文字符串. 思路:回文字符串,第一想到的就是使用两个指针,前后各一个,当遇到前后字符不一致的时候,有两种情况,删除前面字符或者删 ...
随机推荐
- Linux Ubuntu运行线程程序出现undefined reference to ‘pthread_create’和undefined reference to ‘pthread_join’错误。
Linux Ubuntu运行线程程序出现undefined reference to ‘pthread_create’和undefined reference to ‘pthread_join’错误. ...
- LR编写webservice协议接口
转自:http://lovesoo.org/use-loadrunner-call-webservice-interface-testing-optimization-summary.html 本文主 ...
- 去除移动端苹果手机(ios)的input默认样式与input禁止键盘出现的方式
样式: input{-webkit-appearance: none;} 在iPhone plus点击input框出生日期时会出现如下图: 为了去掉下面那条苹果自带的,可以这样处理:在HTML中的in ...
- CentOS7.6配置ip
查看CentOS版本信息 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release (Core) 配置ip [root@loca ...
- hadoop中block副本的放置策略
下面的这种是针对于塔式服务器的副本的放置策略
- Linux虚拟机(CentOS)安装gcc, g++
1. 确保自己的虚拟机联网 点击那个三角形可以选择连接网络 如果还是连不了网,参考https://www.cnblogs.com/xingbo/p/6100554.html 2.联网后,使用命令 ...
- ES5-Array的新增方法
Array.prototype.indexof(value):得到值在数组中的第一个下标 Array.prototype.lastIndexof(value):得到值在数组中的最后一个下标 Array ...
- 虚拟机设置固定IP从而使同一局域网可以访问
没有ifcfg-eth0 时:https://www.cnblogs.com/itboxue/p/11186910.html (1)关机,将网络模式设置成桥接模式 (2)开机 进入 cd /etc/s ...
- C语言入门第十章----结构体
C语言结构体从本质上讲是一种自定义的数据类型,只不过这种数据类型比较复杂,是由int.char .float等基本类型组成的,你可以认为结构体是一种聚合类型. 在实际开发中,我们可以将一组类型不同的. ...
- Python学习第二十七课——写一个和Django框架的自己的框架
MyWeb框架: from wsgiref.simple_server import make_server def application(environ, start_response): pri ...