125. Valid Palindrome

来源:互联网 发布:认识linux服务器 编辑:程序博客网 时间:2024/06/05 18:47

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.

问题:判断一个字符串是否是回文字符串,只考虑字母和数字 忽略空格标点大小写等

思想:指针i 从前开始 指针j总后开始 向中间移动 ,当字符串为空串或者只有一个字符时它是回文的

   public boolean isPalindrome(String s) {        if(s==null||s.length()==1) return true;                int i=0;        int j=s.length()-1;        while(i<=j){        char left=s.charAt(i);        char right=s.charAt(j);        if(!Character.isAlphabetic(left)&&!Character.isDigit(left)){        i++;        }        else if(!Character.isAlphabetic(right)&&!Character.isDigit(right)){        j--;        }        else{        //System.out.println("left="+left+"\tright="+right);        left=Character.toLowerCase(left);        right=Character.toLowerCase(right);        if(left!=right) return false;        i++;j--;        }                }        return true;    }


0 0