Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.git
Note: For the purpose of this problem, we define empty string as valid palindrome.面試
示例 1:
輸入: "A man, a plan, a canal: Panama"
輸出: true
示例 2:
輸入: "race a car"
輸出: false算法
public boolean isPalindrome(String s) {
if (s.isEmpty())
return true;
int begin = 0;
int end = s.length() - 1;
char beginChar, endChar;
while (begin <= end){
beginChar = s.charAt(begin);
endChar = s.charAt(end);
if (!Character.isLetterOrDigit(beginChar)){
begin++;
continue;
}
else if (!Character.isLetterOrDigit(endChar)){
end--;
continue;
}
else {
if (Character.toLowerCase(beginChar) != Character.toLowerCase(endChar))
return false;
else{
begin++;
end--;
}
}
}
return true;
}ide