力扣https://leetcode-cn.com/problems/valid-palindrome/solution/yan-zheng-hui-wen-chuan-by-leetcode-solution/
此题比较简答,因为我做出来了,哈哈
思考总结
- 字符相等怎么判断相等,ASSIC码
'0' | '9' |
48 | 57 |
'a' | 'z' |
97 | 122 |
'A' | 'Z' |
65 | 90 |
- 需要跳过不考核的字符
- 双指针,相向而行
package com.company.myarrays;
public class Solution_5 {
/**
* "0P"
*/
public boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while (left < right) {
while (!isCharAndNum(s.charAt(left)) && left < right) {
left++;
}
char leftCh = s.charAt(left);
while (!isCharAndNum(s.charAt(right)) && right > left) {
right--;
}
char rightCh = s.charAt(right);
if (isEqualChIgnoreCase(leftCh, rightCh)) {
left++;
right--;
continue;
} else {
return false;
}
}
return true;
}
private boolean isEqualChIgnoreCase(char leftCh, char rightCh) {
//任意一个是数字
if ((leftCh >= '0' && leftCh <= '9') || (rightCh >= '0' && rightCh <= '9')) {
if (leftCh == rightCh) {
return true;
} else {
return false;
}
}
//两个都是字母
if (leftCh - rightCh == 32 || leftCh - rightCh == -32 || leftCh == rightCh) {
return true;
}
return false;
}
public boolean isCharAndNum(char ch) {
if ((ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| (ch >= '0' && ch <= '9')) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
System.out.println('0'==48);
}
}