Valid Palindrome

Valid Palindrome

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.

其中可以在以后刷题中用到的:

Character.isLetterOrDigit()
Character.toLowerCase()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Solution {
public boolean isPalindrome(String s) {
if(s==null)
return true;
int len=s.length();
int p=0;
int q=len-1;
while(p<q)
{
while(Character.isLetterOrDigit(s.charAt(p))==false&&p<q)
{
p++;
}
while(Character.isLetterOrDigit(s.charAt(q))==false&&p<q)
{
q--;
}
if(Character.toLowerCase(s.charAt(p))!=Character.toLowerCase(s.charAt(q)))
{
return false;
}
else
{
p++;
q--;
}
}
return true;
}
}