[LeetCode] 125. Valid Palindrome java

/**125. Valid Palindrome 
 * @param s
 * @returnboolean
 */
public boolean isPalindrome(String s) {
    if (s == null || s.length() <= 1)
        return true;
    for (int i=0, j=s.length()-1; i<=j; ) {
        char ch1 = s.charAt(i);
        char ch2 = s.charAt(j);
        if (isValidChar(ch1) && isValidChar(ch2)) {
            if(Character.toLowerCase(ch1) == Character.toLowerCase(ch2)) {
                i++;
                j--;
            } else {
                return false;
            }   
        } else if (!isValidChar(ch1)){
            i++;
        } else if (!isValidChar(ch2)) {
            j--;
        }
    }
    return true;
}
public boolean isValidChar(char ch1) {
    if ((ch1 >= 'a' && ch1 <= 'z') || (ch1 >= 'A' && ch1 <= 'Z') || (ch1 >= '0' && ch1 <= '9'))
        return true;
    return false;

两头依次向里走,边走变比较
Character.toLowerCase(ch)

你可能感兴趣的:(LeetCode)