Java for LeetCode 125 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.

解题思路:

注意题目,忽略大小写,忽略非字母或数字,JAVA实现如下:

    public boolean isPalindrome(String s) {
if(s.length()<=1)
return true;
int left=0,right=s.length()-1;
while(left<right){
if(!Character.isLetterOrDigit(s.charAt(left))){
left++;
continue;
}
if(!Character.isLetterOrDigit(s.charAt(right))){
right--;
continue;
}
if(Character.toUpperCase(s.charAt(left))!=Character.toUpperCase(s.charAt(right)))
return false;
left++;
right--;
}
return true;
}
上一篇:Python 数据处理----对定长数据的处理


下一篇:PHP自学4——通过函数将数组数据输出到html的Table标签中(使用函数的例子)