当前位置:网站首页>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;
}
边栏推荐
- Analysis of protobuf format of real-time barrage and historical barrage at station B
- 栈的经典应用—括号匹配问题
- Opencv learning log 33 Gaussian mean filtering
- F - birthday cake (Shandong race)
- Understand what is a programming language in a popular way
- [exercise-5] (UVA 839) not so mobile (balance)
- [exercise-4] (UVA 11988) broken keyboard = = (linked list)
- Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
- Opencv learning log 31 -- background difference
- [exercise-7] (UVA 10976) fractions again?! (fraction split)
猜你喜欢
X-Forwarded-For详解、如何获取到客户端IP
[exercise-7] crossover answers
B - Code Party (girls' competition)
PySide6 信号、槽
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
Information security - Analysis of security orchestration automation and response (soar) technology
Openwrt source code generation image
Penetration test (8) -- official document of burp Suite Pro
Penetration test (7) -- vulnerability scanning tool Nessus
X-forwarded-for details, how to get the client IP
随机推荐
What is the difficulty of programming?
STM32 how to use stlink download program: light LED running light (Library version)
Alice and Bob (2021 Niuke summer multi school training camp 1)
Opencv learning log 16 paperclip count
Understand what is a programming language in a popular way
socket通讯
【练习-6】(PTA)分而治之
Research Report of peripheral venous catheter (pivc) industry - market status analysis and development prospect prediction
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
Ball Dropping
Opencv learning log 27 -- chip positioning
Penetration test (8) -- official document of burp Suite Pro
Auto.js入门
Essai de pénétration (1) - - outils nécessaires, navigation
Perform general operations on iptables
CEP used by Flink
【练习-2】(Uva 712) S-Trees (S树)
PySide6 信号、槽
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus