1 题目描述
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: “A man, a plan, a canal: Panama”
输出: true
示例 2:
输入: “race a car”
输出: false
2 注意事项
看清题目要求,只考虑字母和数字字符
class Solution {
public boolean isPalindrome(String s) {
s = s.toLowerCase();
int low = 0, high = s.length()-1;
while (low < high) {
String a = s.charAt(low)+"";
while (!a.matches("[a-z|0-9]")) {
low++;
if (low >= high) return true;
a = s.charAt(low)+"";
}
a = s.charAt(high)+"";
while (!a.matches("[a-z|0-9]")) {
high--;
if (low >= high) return true;
a = s.charAt(high)+"";
}
if (s.charAt(low++) != s.charAt(high--)) return false;
}
return true;
}
}