当前位置:网站首页>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
边栏推荐
- 你还不知道责任链模式的使用场景吗?
- Achievements in science and Technology (21)
- 《JVM系列》 第七章 -- 字节码执行引擎
- 自动化的艺术
- 1分钟看懂执行流程,永久掌握for循环(附for循环案例)
- Model location setting in GIS data processing -cesium
- AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
- The initial C language framework is suitable for review and preliminary understanding
- 直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
- MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
猜你喜欢

Arduino for esp8266串口功能简介

交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)

成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍

外包幹了四年,廢了...

PCIe X1 插槽的主要用途是什么?

MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program

Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)

你还不知道责任链模式的使用场景吗?

c语言在结构体传参时参数压栈问题

【无标题】
随机推荐
初识STL
SVN详细使用教程
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
抑郁症治疗的进展
Outsourcing for four years, abandoned
Four ways of SSH restricting login
Chapter 7 of JVM series -- bytecode execution engine
8.5 循环神经网络简洁实现
矩阵交换行列
UML project example -- UML diagram description of tiktok
QT interface optimization: QT border removal and form rounding
Svn detailed use tutorial
UML项目实例——抖音的UML图描述
Sed learning for application
Solve the problem of SSH configuration file optimization and slow connection
自动化的艺术
raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
全连接层的作用是什么?
AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等