当前位置:网站首页>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-06-10 00:50:00 【Small white yards fly up】
Summary
It's still two finger needling , And deleting at most one character is the key !
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 .
Ideas
Direct double pointer traverses from both ends to the middle , Compare the two characters for consistency . In the stem of the question Delete at most one character It's a key . If we encounter character inconsistencies , Do we delete the left pointer or the right pointer ? Try both , See whether the substring formed after deleting two pointers is a palindrome string . If not , shows Delete at most one character It's impossible .
solution : Double pointer + Determine whether the substring is a palindrome at one time
Code
public boolean validPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return isPalindrome(s.substring(left + 1, right + 1))
|| isPalindrome(s.substring(left, right));
}
left++;
right--;
}
return true;
}
public boolean isPalindrome(String s) {
for (int i = 0; i <= s.length() / 2 - 1; i++) {
if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
return false;
}
}
return true;
}
边栏推荐
猜你喜欢

OSPF first experiment

Typera basic use and change the theme style of typera
![The binary tree is expanded into a linked list [the essence of tree processing -- how to handle each sub tree]](/img/b3/5a7c20fad1fd17ef82ed730d686723.png)
The binary tree is expanded into a linked list [the essence of tree processing -- how to handle each sub tree]

Benders decompositon学习笔记记录

OSPF第一次实验

University of Ulm, Germany | comparative characterization of 3D protein structure

Gartner global IAAs report in 2021: AWS cake is nibbled, and Chinese cloud manufacturers are steadily attacking

IDC fait autorité pour prédire que l'industrie manufacturière chinoise est sur le point de monter dans le nuage

Go profile management -viper

IDC權威預測,中國制造業即將乘雲而上
随机推荐
试题 历届真题 完全二叉树的权值【第十届】【省赛】【B组】
WPS how to adjust the position of text after merging cells
RHCSA第三天
劍指 Offer II 018. 有效的回文
wps合并单元格后怎么将文字的位置进行调整
Go zero micro Service Practice Series (II. Service splitting)
Offre de doigts II 018. Palindrome valide
Cloud Mining & cloud mining chain: from order collaboration to procurement supply chain, make procurement supply chain interconnected
RHCSA第一天
Transformer
剑指 Offer II 020. 回文子字符串的个数
Code case - web version confession wall and file upload
线性规划和对偶规划学习总结
内网渗透第4章
ospf总结
剑指 Offer II 011. 0 和 1 个数相同的子数组
Weights of complete binary tree of past real questions [10th] [provincial competition] [group B]
Minimum toll
hcip第一次作业
修复win10的微软应用商店闪退