首页 > 代码库 > leetcode Valid Palindrome

leetcode 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.

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. 



简单题,输入一个字符串,不考虑数字、字母以外的任何符号。

判断是否是回文串输出即可。


设置双指针查询即可。


public class Solution {
    public boolean isPalindrome(String s) {
        s=s.toLowerCase();
        int len = s.length() -1;
        if(len==-1)
            return true;
        for(int i=0,j=len;i<=j;i++,j--){
            for(;!((s.charAt(i)>='0'&&s.charAt(i)<='9')||(s.charAt(i)>='a'&&s.charAt(i)<='z'))&&i<j;i++);
            for(;!((s.charAt(j)>='0'&&s.charAt(j)<='9')||(s.charAt(j)>='a'&&s.charAt(j)<='z'))&&i<j;j--);

            if(s.charAt(i)!=s.charAt(j))
                return false;
        }
        return true;
    }
}


leetcode Valid Palindrome