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) { if (s==null||s.length()==0) { return true; } char a = 'a'; char z = 'z'; int begin = 0,end = s.length()-1; //这里只能用一次不能再循环里调用 不然会TLE char[] chars = s.toCharArray(); while (begin<=end) { char charBegin = chars[begin]; //处理大小写转换问题,统一转小写 if (charBegin>='A'&&charBegin<='Z') { charBegin = (char) (charBegin + 32); } char charEnd = chars[end]; if (charEnd>='A'&&charEnd<='Z') { charEnd = (char) (charEnd + 32); } //不是可用的字符跳过 注意边界没有 = if (charBegin < a || charBegin > z) { if (charBegin<'0' || charBegin>'9') { begin++; continue; } } if (charEnd < a || charBegin > z) { if (charEnd<'0' || charEnd>'9') { end--; continue; } } if (charBegin != charEnd) { return false; } begin++;end--; } return true; } }