当前位置:网站首页>Sword finger offer II 019 Delete at most one character to get a palindrome
Sword finger offer II 019 Delete at most one character to get a palindrome
2022-07-06 16:08:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/RQku0D/
This question is related to 680. Verify palindrome string Ⅱ identical
subject :
Given a non empty string s, Please judge if most If you delete a character from the string, you can get a palindrome string .
Example 1:
| Input : s = "aba" Output : true |
Example 2:
| Input : s = "abca" Output : true explain : You can delete "c" character perhaps "b" character |
Example 3:
| Input : s = "abc" Output : false |
Tips :
| 1 <= s.length <= 105 s It's made up of lowercase letters |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/RQku0D
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
Here are some examples :
Palindrome :abba,abcba
It can be seen that as long as palindromes are deleted 1 Times are still palindromes
Non palindrome :abcb,bcba,cbbcc
The left pointer points to [0], The right pointer points to [len-1], Adjust the left or right pointer once , If the rest is palindromes, then you can
Let's take a longer example :ebcbbececabbacecbbcbe
The left pointer can be adjusted [5] ->[6], The right pointer cannot be adjusted [15]->[14], The total number of adjustments is also one
So the conclusion is , Scan the entire string with double pointers , If it's palindrome perhaps After one adjustment, the substring is also a palindrome , Then the result is ok , Otherwise, you can't
bool checksub(char *s, int i, int j)
{
bool ret = true;
while(i < j)
{
if(s[i] != s[j])
{
ret = false;
break;
}
i++;
j--;
}
return ret;
}
bool validPalindrome(char * s){
int slen = strlen(s);
int i,j;
int maxjudge = 0;
bool ret = true;
i = 0;
j = slen - 1;
while(i < j)
{
if(s[i] != s[j])
{
if( checksub(s, i, j-1) || checksub(s, i+1, j) )
{
ret = true;
break;
}
else
{
ret = false;
break;
}
}
i++;
j--;
}
return ret;
}边栏推荐
- Record of brushing questions with force deduction -- complete knapsack problem (I)
- Information security - security professional name | CVE | rce | POC | Vul | 0day
- [teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
- The concept of C language array
- Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
- B - Code Party (girls' competition)
- Nodejs+vue online fresh flower shop sales information system express+mysql
- Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
- C language learning notes
- Data storage in memory & loading into memory to make the program run
猜你喜欢

树莓派4B安装opencv3.4.0
![[exercise-7] crossover answers](/img/66/3dcba2e70a4cd899fbd78ce4d5bea2.png)
[exercise-7] crossover answers
Quick to typescript Guide

C language learning notes

信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志

2027. Minimum number of operations to convert strings

Nodejs+vue网上鲜花店销售信息系统express+mysql
![mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’](/img/e6/f4a696179282fe1f4193410c5a493a.png)
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’

渗透测试 ( 1 ) --- 必备 工具、导航

Penetration test (3) -- Metasploit framework (MSF)
随机推荐
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
想应聘程序员,您的简历就该这样写【精华总结】
Quick to typescript Guide
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Opencv learning log 31 -- background difference
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
Information security - security professional name | CVE | rce | POC | Vul | 0day
Opencv learning log 19 skin grinding
X-forwarded-for details, how to get the client IP
Opencv learning log 29 -- gamma correction
Frida hook so layer, protobuf data analysis
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
信息安全-安全编排自动化与响应 (SOAR) 技术解析
CEP used by Flink
PySide6 信号、槽
Record of brushing questions with force deduction -- complete knapsack problem (I)
【练习-9】Zombie’s Treasure Chest
Matlab comprehensive exercise: application in signal and system
Opencv learning log 13 corrosion, expansion, opening and closing operations