当前位置:网站首页>Sword finger offer II 018 Valid palindrome
Sword finger offer II 018 Valid palindrome
2022-06-10 00:50:00 【Small white yards fly up】
A word summary
Double pointers traverse from both ends to the center at the same time , If not, return to false.
subject
Given a string s , verification s Whether it is Palindrome string , Consider only alphabetic and numeric characters , The case of letters can be ignored .
In this question , Define an empty string as a valid Palindrome string .

Ideas
We need to ignore characters other than letters and numbers .
You can traverse each character of the string first , Do a pretreatment first . Preprocessing needs to remove non alphanumeric characters , Then unify the upper case letters into lower case .
Then use two pointers to traverse from both sides to the center , Check whether the elements corresponding to the two pointers are consistent .
This is where ASCII code , Refer to the sword finger Offer II 002. The extended knowledge of binary addition .
Solution 1 : Preprocessing strings , The double pointer traverses toward the center
Code
public boolean isPalindrome(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
sb.append(c);
}
}
String finalStr = sb.toString().toLowerCase();
if ("".equals(finalStr)) {
return true;
}
for (int i = 0; i <= finalStr.length() / 2 - 1; i++) {
if (finalStr.charAt(i) != finalStr.charAt(finalStr.length() - i - 1)) {
return false;
}
}
return true;
}
Solution 2 : Direct double pointer traversal to the center
Logic
For the first solution , You can start double pointer traversal to the center directly , A pretreatment process is omitted , While traversing , To determine whether it is a number or a letter .
Code
public boolean isPalindrome(String s) {
String s1 = s.toLowerCase();
int left = 0;
int right = s1.length() - 1;
while (left < right) {
char leftC = s1.charAt(left);
char rightC = s1.charAt(right);
if (needRemove(leftC)) {
left++;
continue;
}
if (needRemove(rightC)) {
right--;
continue;
}
if (leftC != rightC) {
return false;
}
left++;
right--;
}
return true;
}
public boolean needRemove(char c){
return (c < '0' || c > '9') && (c < 'a' || c > 'z');
}
边栏推荐
- ospf总结
- Several syntax for adding events to elements in a page
- 1049 robber Ah Fu
- Gartner global IAAs report in 2021: AWS cake is nibbled, and Chinese cloud manufacturers are steadily attacking
- 二分查找(折半查找)总结
- Republish experiment
- hcip第一次作业
- 剑指 Offer II 014. 字符串中的变位词
- 力扣 两数相加 C语言 题解
- Solution to the C language problem of the sum of two numbers
猜你喜欢

Republish experiment
![[GoogleCTF2019 Quals]Bnv -S](/img/85/b8ed2ed1b3a5484007dba0f7c0eb55.png)
[GoogleCTF2019 Quals]Bnv -S

Go zero micro Service Practice Series (II. Service splitting)

Rhcsa day 1

ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks

wps合并单元格内容都保留在一个单元格怎么实现

Pycharm 2022 permanently activated version download, valid for personal test

剑指 Offer II 013. 二维子矩阵的和

收藏备用 | 关于OAuth2的一些常见问题总结

劍指 Offer II 018. 有效的回文
随机推荐
How WPS merges cells with different sizes
if判斷是否為空時的函數選擇
Minimum toll
Pycharm 2022 permanently activated version download, valid for personal test
MySQL execution plan
run npm fund for details
工程伦理课后习题答案
mpls vpn
线性规划和对偶规划学习总结
Work sharing of 2018 virtual instrument competition - reconfigurable snake shaped robot based on LabVIEW, baidu map, STM32 single chip microcomputer, etc
Transformer
typora 基本使用和更换typora的主题样式
nn.ModuleList()与nn.Sequential()
go配置文件管理-viper
Benders decompositon学习笔记记录
IDC权威预测,中国制造业即将乘云而上
CloudCompare&PCL 主曲率、平均曲率以及高斯曲率计算
Several syntax for adding events to elements in a page
OSPF实验
Code case - web version confession wall and file upload