Description:
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.
Solution:
将所有字母和数字的char取出,然后判断即可。
import java.util.*;
public class Solution {
public boolean isPalindrome(String s) {
int len = s.length();
char ch[] = new char[len];
s = s.toLowerCase();
int tot = 0;
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
ch[tot++] = c;
}
for (int i = 0; i < tot; i++)
if (ch[i] != ch[tot - 1 - i])
return false;
return true;
}
}