当前位置:网站首页>剑指 Offer II 019. 最多删除一个字符得到回文(简单)
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
2022-04-23 14:35:00 【重you小垃】
思路:双指针,将复杂度降到O(n)
具体思路:l=0 r=n-1 如果s[l]==s[r]则l++;r–; 否则只要s[l+1,r]或者s[l,r-1]是回文即可
class Solution {
public:
bool valid(const string &s, int l, int r) {
while (l < r) {
if (s[l] != s[r]) return false;
l++;r--;
}
return true;
}
bool validPalindrome(string s) {
int l =0, r = s.size() - 1;
while (l < r) {
if (s[l] == s[r]) {
l++;
r--;
}else {
return valid(s, l + 1, r) || valid(s, l, r - 1);
}
}
return true;
}
};
版权声明
本文为[重you小垃]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zhangjiaji111/article/details/124359822
边栏推荐
猜你喜欢
MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
L'externalisation a duré quatre ans.
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
《JVM系列》 第七章 -- 字节码执行引擎
Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34
爬虫练习题(一)
Arduino for esp8266串口功能简介
ASEMI超快恢复二极管与肖特基二极管可以互换吗
本以为能躺着进华为,结果陆续收到京东/滴滴/爱奇艺offer的我迷茫了
随机推荐
555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
redis 模块编程中 key value的生命周期
Notes on Visio drawing topology
Qt实战:云曦聊天室篇
C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【1】
Qt实战:云曦日历篇
Solve the problem of SSH configuration file optimization and slow connection
8.4 循环神经网络从零实现
8.5 循环神经网络简洁实现
1N5408-ASEMI整流二极管1N5408
MDS55-16-ASEMI整流模块MDS55-16
8.3 语言模型与数据集
tcp_diag 内核相关实现 1 调用层次
Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
Detailed explanation of C language P2 selection branch statement
LLVM - 生成 if-else 以及 PH
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
Qt界面优化:鼠标双击特效
关于在vs中使用scanf不安全的问题