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; }
时间: 2024-11-14 20:01:34