当前位置:网站首页>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;
}边栏推荐
- Opencv learning log 26 -- detect circular holes and mark them
- China potato slicer market trend report, technical dynamic innovation and market forecast
- 初入Redis
- 树莓派4B安装opencv3.4.0
- 【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
- [exercise-6] (PTA) divide and conquer
- Understand what is a programming language in a popular way
- Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
- Write web games in C language
- CEP used by Flink
猜你喜欢

渗透测试 ( 1 ) --- 必备 工具、导航
![[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class](/img/3b/dc43564a36f82e73826b08f39c935e.png)
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class

渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集

Pyside6 signal, slot

Write web games in C language

滲透測試 ( 1 ) --- 必備 工具、導航

B - 代码派对(女生赛)

Information security - threat detection - detailed design of NAT log access threat detection platform

【练习-5】(Uva 839)Not so Mobile(天平)
Frida hook so layer, protobuf data analysis
随机推荐
滲透測試 ( 1 ) --- 必備 工具、導航
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
Pyside6 signal, slot
[exercise-6] (UVA 725) division = = violence
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
Opencv learning log 28 -- detect the red cup cover
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
China potato slicer market trend report, technical dynamic innovation and market forecast
Data storage in memory & loading into memory to make the program run
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
【高老师UML软件建模基础】20级云班课习题答案合集
2027. Minimum number of operations to convert strings
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
Openwrt source code generation image
Opencv learning log 30 -- histogram equalization
【练习-8】(Uva 246)10-20-30==模拟
【练习-6】(PTA)分而治之
Penetration test (8) -- official document of burp Suite Pro