当前位置:网站首页>剑指 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
边栏推荐
猜你喜欢
8.4 循环神经网络从零实现
如何5分钟上手使用OCR
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
外包幹了四年,廢了...
51单片机的直流电机PWM调速控制系统(附Proteus仿真+C程序等全套资料)
8.2 文本预处理
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
MySQL报错packet out of order
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
随机推荐
flannel 原理 之 子网划分
如何5分钟上手使用OCR
Qt实战:云曦聊天室篇
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
顺序栈的基本操作
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
Logical volume creation and expansion
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
Master in minutes --- ternary operator (ternary operator)
LLVM - 生成 if-else 以及 PH
GIS数据处理-cesium中模型位置设置
一款不错的工具:aardio
MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
Branch statement of process control
Four ways of SSH restricting login
PWM speed regulation control system of DC motor based on 51 single chip microcomputer (with complete set of data such as Proteus simulation + C program)
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换