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..
先扫一遍忽略空格,塞到char[]里面,从两端检测
public class Solution {
public boolean isPalindrome(String s)
{
int len=s.length();
if(len==0)
return true;
char[] carr=new char[len];
int cnt=0;
for(int i=0;i<len;i++)
{
char c=s.charAt(i);
if(c>='0'&&c<='9'||c>='a'&&c<='z'||c>='A'&&c<='Z')
carr[cnt++]=c;
}
String str=new String(carr, 0, cnt);
str=str.toLowerCase();
int size=str.length();
for(int i=0,j=size-1;i<size;i++,j--)
if(str.charAt(i)!=str.charAt(j))
return false;
return true;
}
}