当前位置:网站首页>Sword finger offer II 019 Delete at most one character to get palindrome (simple)
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
2022-04-23 14:40:00 【Heavy garbage】
Ideas : Double pointer , Reduce the complexity to O(n)
Specific ideas :l=0 r=n-1 If s[l]==s[r] be l++;r–; Otherwise, as long as s[l+1,r] perhaps s[l,r-1] It's palindrome
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;
}
};
版权声明
本文为[Heavy garbage]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231435304495.html
边栏推荐
- DVWA之暴力破解(Brute Force)Low-->high
- 在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
- 51单片机的直流电机PWM调速控制系统(附Proteus仿真+C程序等全套资料)
- Solve the problem of SSH configuration file optimization and slow connection
- The art of automation
- Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
- 你還不知道責任鏈模式的使用場景嗎?
- I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
- MDS55-16-ASEMI整流模块MDS55-16
- 线程同步、生命周期
猜你喜欢
On the insecurity of using scanf in VS
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
Don't you know the usage scenario of the responsibility chain model?
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
编程哲学——自动加载、依赖注入与控制反转
C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【1】
查找水仙花数-for循环实践
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
随机推荐
如何打开Win10启动文件夹?
async void 导致程序崩溃
OpenFaaS实战之四:模板操作(template)
详解TCP的三次握手
QT interface optimization: QT border removal and form rounding
在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
On the insecurity of using scanf in VS
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
查找水仙花数-for循环实践
Some little records~
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
Unity_代码方式添加绑定按钮点击事件
8.4 循环神经网络从零实现
Find daffodils - for loop practice
2-Go变量操作
ASEMI整流模块MDQ100-16在智能开关电源中的作用
【Proteus仿真】自动量程(范围<10V)切换数字电压表
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等