125.验证回文串
# 125.验证回文串
LeetCode: https://leetcode.cn/problems/valid-palindrome/ (opens new window)
题目描述: 验证只考虑字母和数字字符的回文串
输入: "A man, a plan, a canal: Panama"
输出: true
解释:"amanaplanacanalpanama" 是回文串
- 声明左右指针
- 将左右指针移动到有效的符号位置
- 开始比较
public boolean isPalindrome(String s) {
char[] chars = s.toLowerCase().toCharArray();
int left = 0;
int right = chars.length - 1;
while (left < right){
while (left < right && !Character.isLetterOrDigit(chars[left])) {
left++;
}
while (left < right && !Character.isLetterOrDigit(chars[right])) {
right--;
}
if (chars[left] == chars[right]){
left++;
right--;
}else {
return false;
}
}
return true;
}