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.
只考虑数字和字母,字母大小写都一样,判断是否是回文。重点是忽略非非字母和数字。比较简单。
public class Solution {
public boolean isPalindrome(String s) { int low=0;
int high=s.length()-1;
String s1=s.toLowerCase();
while(low<high)
{ while(low<high)
{
char c=s1.charAt(low);
if((c>='a'&&c<='z')||(c>='0'&&c<='9'))
{
break;
}
else low++; } while(low<high)
{
char c=s1.charAt(high);
if((c>='a'&&c<='z')||(c>='0'&&c<='9'))
{
break;
}else high--; } if(s1.charAt(low)==s1.charAt(high))
{
low++;
high--;
}
else
{
return false;
} } return true; }
}