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;
    }

 

posted @ 2015-05-26 17:43  TonyLuis  阅读(218)  评论(0编辑  收藏  举报