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.
class Solution {
public boolean isPalindrome(String s) {
char t[] = s.toCharArray();
char a[] = new char[t.length];
int l=0;
for(int i=0;i<t.length;i++){
if(t[i]>='a'&&t[i]<='z'||t[i]>='A'&&t[i]<='Z'||t[i]>='0'&&t[i]<='9'){
if(t[i]>='A'&&t[i]<='Z')
a[l]= Character.toLowerCase(t[i]);
else
a[l]= t[i];
l++;
}
}
for(int i=0,j=l-1;i<=j;i++,j--){
if(a[i]!=a[j])
return false;
}
return true;
}
}

本文介绍了一种判断字符串是否为回文的有效算法。该算法仅考虑字母数字字符,并忽略大小写差异。通过示例说明了如何实现这一算法,包括如何处理空字符串等特殊情况。

被折叠的 条评论
为什么被折叠?



