"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
该题目的有两点需要注意:
1. 大小写转换。
2. 无用字符删除。
这两个问题使用java的正则表达式可以轻易解决。
public class Solution {
public boolean isPalindrome(String s) {
if(s == null){
return true;
}
s = s.toUpperCase().replaceAll("[^0-9a-zA-Z]", "");
int begin = 0;
int end = s.length() - 1;
while(begin < end){
if(s.charAt(begin++) != s.charAt(end--)){
return false;
}
}
return true;
}
}