当前位置:网站首页>Leetcode(680)——验证回文字符串 Ⅱ
Leetcode(680)——验证回文字符串 Ⅱ
2022-06-29 23:02:00 【SmileGuy17】
Leetcode(680)——验证回文字符串 Ⅱ
题目
给定一个非空字符串 s,最多删除一个字符。判断是否能成为回文字符串。
示例 1:
输入: s = “aba”
输出: true
示例 2:
输入: s = “abca”
输出: true
解释: 你可以删除c字符。
示例 3:
输入: s = “abc”
输出: false
提示:
- 1 1 1 <= s.length <= 1 0 5 10^5 105
- s 由小写英文字母组成
题解
方法一:贪心(双指针)
思路
考虑最朴素的方法:首先判断原串是否是回文串,如果是,就返回 true \text{true} true;如果不是,则枚举每一个位置作为被删除的位置,再判断剩下的字符串是否是回文串。这种做法的渐进时间复杂度是 O ( n 2 ) O(n^2) O(n2)的,会超出时间限制。
我们换一种想法。首先考虑如果不允许删除字符,如何判断一个字符串是否是回文串。常见的做法是使用双指针。定义左右指针,初始时分别指向字符串的第一个字符和最后一个字符,每次判断左右指针指向的字符是否相同,如果不相同,则不是回文串;如果相同,则将左右指针都往中间移动一位,直到左右指针相遇,则字符串是回文串。
在允许最多删除一个字符的情况下,同样可以使用双指针,通过贪心实现。初始化两个指针 low \textit{low} low 和 high \textit{high} high 分别指向字符串的第一个字符和最后一个字符。每次判断两个指针指向的字符是否相同,如果相同,则更新指针,将 low \textit{low} low 加 1 1 1, high \textit{high} high 减 1 1 1,然后判断更新后的指针范围内的子串是否是回文字符串。如果两个指针指向的字符不同,则两个字符中必须有一个被删除,此时我们就分成两种情况:即删除左指针对应的字符,留下子串 s [ low + 1 : high ] s[\textit{low} + 1 : \textit{high}] s[low+1:high],或者删除右指针对应的字符,留下子串 s [ low : high − 1 ] s[\textit{low} : \textit{high} - 1] s[low:high−1]。当这两个子串中至少有一个是回文串时,就说明原始字符串删除一个字符之后就以成为回文串。
贪心策略是:左右指针遇到不同字符,则删除其中一个,然后继续遍历。

代码实现
我自己的
class Solution {
bool checkValidPalindrome(string& s, int l, int r){
while(l < r){
if(s[l] == s[r]){
l++;
r--;
}else return false;
}
return true;
}
public:
bool validPalindrome(string s) {
// 反向双指针
int l = 0, r = s.size()-1;
while(l < r){
if(s[l] == s[r]){
l++;
r--;
}else return checkValidPalindrome(s, l+1, r) || checkValidPalindrome(s, l, r-1);
// 因为不确定哪个是阻止回文字符串的字符串,所以都遍历,错误的那个很快就会退出函数,消耗时间不长
}
return true;
}
};
复杂度分析
时间复杂度: O ( n ) O(n) O(n),其中 nn 是字符串的长度。判断整个字符串是否是回文字符串的时间复杂度是 O ( n ) O(n) O(n),遇到不同字符时,判断两个子串是否是回文字符串的时间复杂度也都是 O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)。只需要维护有限的常量空间
边栏推荐
- matplotlib matplotlib可视化之柱状图plt.bar()
- Procurement intelligence is about to break out, and the "3+2" system of Alipay helps enterprises build core competitive advantages
- Touch key and key control corresponding LED status reversal
- Laravel creates its own facade extension geoip to obtain country, region and city information according to IP
- label问题排查:打不开标注好的图像
- Pytest initializing and cleaning up the environment
- 微博系统中”微博评论“的高性能高可用计算架构
- redis客户端
- 服务器快速搭建AList集成网盘网站【宝塔面板一键部署AList】
- Vs cannot locate program input point to DLL
猜你喜欢
Evolution from stand-alone to distributed database storage system

Status acquisition and control system of on-site express cabinet

Qdomdocument and qdomnode are used in QT to read XML

Design of Distributed Message Oriented Middleware

分布式消息中间件设计

wirehark数据分析与取证infiltration.pacapng

服务器快速搭建AList集成网盘网站【宝塔面板一键部署AList】

sql刷题595. 大的国家

Ansible automatic operation and maintenance

论文阅读《Large-Scale Direct SLAM with Stereo Cameras》
随机推荐
股票开户安全吗?上海股票开户。
啃下大骨头——排序(一)
写论文工具:LaTex在线网站
Talk about auto in MySQL in detail_ What is the function of increment
sql刷题595. 大的国家
High performance and high availability computing architecture of "Weibo comments"
Status acquisition and control system of on-site express cabinet
提供有效的绩效评估
优雅的改造短信业务模块,策略模式走起!
关于二叉树
Processing of error b6267342 reported by AIX small machine in production environment
疫情下我离职一年,收入增长了10倍
Project 1 - buffer pool [cmu 15-445645] notes
Database - playful data -pgsql uses UUID as primary key
Solr基础操作4
Node data collection and remote flooding transmission of label information
Mysql database: use the show profile command to analyze performance
Still stay up late every day and work overtime to make statements? In fact, you don't know how to make reports efficiently
Hematemesis finishing: a rare map of architects!
均值、方差、标准差、协方差的概念及意义