Valid Palindrome
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.
Java代码:public class Solution {
public boolean isPalindrome(String s) {
int start =0;
int end = s.length()-1;
while(start < end){
while((s.charAt(start)<'A' || s.charAt(start)>'z' || (s.charAt(start)>'Z' && s.charAt(start)<'a')) && start<s.length()-1 && (s.charAt(start)<'0' || s.charAt(start)>'9'))
start++;
while((s.charAt(end)<'A' || s.charAt(end)>'z' || (s.charAt(end)>'Z' && s.charAt(end)<'a')) && end>0 && (s.charAt(end)<'0' || s.charAt(end)>'9'))
end--;
if(start < end){
if(s.charAt(start) == s.charAt(end) ||s.charAt(start)+32 == s.charAt(end) || s.charAt(start)-32 == s.charAt(end)){
start++;
end--;
}else{
return false;
}
}
}
return true;
}
}