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
题目:给定一个字符串,在最多删除一个字符的情况下,判断这个字符串是不是回文字符串. 思路:回文字符串,第一想到的就是使用两个指针,前后各一个,当遇到前后字符不一致的时候,有两种情况,删除前面字符或者删 ...
随机推荐
- 67课 for循环1-为什么需要for循环
# include <stdio.h> int main (void) { int i; ;.//sum代表和的意思 ; i<=; ++i)//第八行代码 sum = sum + i ...
- Selenium元素定位之页面检测技巧
我们在进行web自动化测试的时候进行XPath或者CSS定位,需要检测页面元素定位是否正确,如果用脚本去检测,那么效率是极低的. 一般网上推选装额外的插件来实现页面元素定位检测 如:firebug. ...
- linux开启端口命令
1. 开放端口命令: /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT 2.保存:/etc/rc.d/init.d/iptables save ...
- java篇 之 多态
2018-9-28 多态: 重载也称为静态多态(静态在编译阶段就能确定)(动态是跟运行时挂钩) 尽量去选择关系轻的,降低耦合度(紧密度) 内聚: 减少与外界的联系,降低与其他对象和类的联系 对象与对象 ...
- CUP监测1分钟(60s)的python的matplotlib动态作图
import matplotlib.pyplot as plt import psutil import time # 第1步,导出通用字体设置 from matplotlib import font ...
- Python学习(一)——开发语言和Python的安装
开发语言: 高级语言: Python,Java,PHP,C#,Go,ruby,C++...都依赖于C→字节码 语言的对比: Python,Java:既能写网页又能写后台 Python:开发效率比Jav ...
- Springboot项目搭建(1)-创建,整合mysql/oracle,druid配置,简单的CRUD
源码地址:https://github.com/VioletSY/article-base 1:创建一个基本项目:https://blog.csdn.net/mousede/article/detai ...
- 广度优先搜索(BFS)与深度优先搜索(DFS)的对比及优缺点
深搜,顾名思义,是深入其中.直取结果的一种搜索方法. 如果深搜是一个人,那么他的性格一定倔得像头牛!他从一点出发去旅游,只朝着一个方向走,除非路断了,他绝不改变方向!除非四个方向全都不通或遇到终点,他 ...
- 使用python实现归并排序、快速排序、堆排序
归并排序 使用分治法:分而治之 分: 递归地拆分数组,直到它被分成两对单个元素数组为止. 然后,将这些单个元素中的每一个与它的对合并,然后将这些对与它们的对等合并,直到整个列表按照排序顺序合并为止. ...
- 探讨 Git 代码托管平台的若干问题
关于 Git 版本控制软件种类繁多,维基百科收录的最早的版本控制系统是 1972 年贝尔实验室开发的 Source Code Control System.1986 年 Concurrent Vers ...