125. Valid Palindrome

一开始以为只有字母,结果是数字和字母,挂了好几次。

class Solution {
    public boolean isPalindrome(String s) {
        int hi = s.length()-1;
        int lo = 0 ;
        while(lo<=hi)
        {
            while(lo<=hi&&!isAlphanumeric(s.charAt(lo)))
                lo++;
            while(hi>=lo&&!isAlphanumeric(s.charAt(hi)))
                hi--;
            if(hi==lo-1)
                return true;
            char ch1 = Character.toLowerCase(s.charAt(lo));
            char ch2 = Character.toLowerCase(s.charAt(hi));
            if(ch1!=ch2) return false;
            hi--;
            lo++;
        }
        return true;
    }
    private boolean isAlphanumeric (char ch)
    {
        return (ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9');
    }
}

你可能感兴趣的:(125. Valid Palindrome)