当前位置:网站首页>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;
}边栏推荐
- C language is the watershed between low-level and high-level
- 【练习-5】(Uva 839)Not so Mobile(天平)
- Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
- Essai de pénétration (1) - - outils nécessaires, navigation
- Gartner: five suggestions on best practices for zero trust network access
- Basic Q & A of introductory C language
- 渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
- 初入Redis
- Programmers, what are your skills in code writing?
- 信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
猜你喜欢
Quick to typescript Guide

D - function (HDU - 6546) girls' competition
快速转 TypeScript 指南

Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability

628. Maximum product of three numbers

Penetration test (8) -- official document of burp Suite Pro

Gartner: five suggestions on best practices for zero trust network access

Ball Dropping

2027. Minimum number of operations to convert strings
![[exercise-7] crossover answers](/img/66/3dcba2e70a4cd899fbd78ce4d5bea2.png)
[exercise-7] crossover answers
随机推荐
Matlab comprehensive exercise: application in signal and system
Gartner: five suggestions on best practices for zero trust network access
Write web games in C language
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
TCP's three handshakes and four waves
Interval sum ----- discretization
Interesting drink
Opencv learning log 12 binarization of Otsu method
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
[exercise-6] (PTA) divide and conquer
628. Maximum product of three numbers
Data storage in memory & loading into memory to make the program run
Opencv learning log 18 Canny operator
F - birthday cake (Shandong race)
渗透测试 ( 1 ) --- 必备 工具、导航
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
[exercise-5] (UVA 839) not so mobile (balance)
Ball Dropping
C language must memorize code Encyclopedia
【高老师软件需求分析】20级云班课习题答案合集