给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: "A man, a plan, a canal: Panama" 输出: true
示例 2:
输入: "race a car" 输出: false
class Solution {
public boolean isPalindrome(String s) {
if(s==null || s.length() ==0) {
return true;
}
s = s.toLowerCase();
int start = 0;
int end = s.length() -1;
while(start<end) {
char c1 = s.charAt(start);
char c2 = s.charAt(end);
if(!((c1 >= 'a' && c1 <= 'z') || (c1 >= '0' && c1 <= '9'))) {
start++;
continue;
}
if(!((c2 >= 'a' && c2 <= 'z') || (c2 >= '0' && c2 <= '9'))) {
end--;
continue;
}
if(c1!=c2) {
return false;
}
start++;
end--;
}
return true;
}
}