Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama"
is a palindrome.
"race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
思路:
1.回文数的问题,一般可以采用两个参数start与end来解决,判断两个参数对应的字符是否满足要求即可
2.一般采用while循环,条件是start<end;内部循环也采用while,对于start只要不是数字或者字符,start后移一位;
对于end,只要不是数字或者字符,前移一位
3 比较start和end对应的字符,不满足所有要求则为false,否则,循环继续,start后移,end前移
public boolean isPalindrome(String s) {
if(s==null||"".equals(s.trim())) return s==null?false:true;
String a=s.trim();
int start=0;
int end=a.length()-1;
while(start<end){
while(start<end&&(a.charAt(start)<'0'||(a.charAt(start)>'9'&&a.charAt(start)<'A')||(a.charAt(start)>'Z'&&a.charAt(start)<'a')||a.charAt(start)>'z')){
start++;
}
while(start<end&&(a.charAt(end)<'0'||(a.charAt(end)>'9'&&a.charAt(end)<'A')||(a.charAt(end)>'Z'&&a.charAt(end)<'a')||a.charAt(end)>'z')){
end--;
}
if(a.charAt(start)!=a.charAt(end)&&a.charAt(start)!=a.charAt(end)-32&&a.charAt(start)!=a.charAt(end)+32)
return false;
else{
start++;
end--;
}
}
return true;
}