马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
class Solution {
public static boolean isPalindrome(String s) {
if(s.length() == 0) return true;
int start = 0;
int end = s.length() -1;
char[] arr = s.toCharArray();
while(end - start >= 1){
if(Character.isLetterOrDigit(arr[start]) && Character.isLetterOrDigit(arr[end])){
if(Character.toLowerCase(arr[start]) != Character.toLowerCase(arr[end])) return false;
start++;
end--;
}
else{
if(!Character.isLetterOrDigit(arr[start])) start++;
if(!Character.isLetterOrDigit(arr[end])) end--;
}
}
return true;
}
}
|