125. Valid Palindrome

简单的two pointers, 考虑一下边界: “”, "a"都是直接return true

public boolean isPalindrome(String s) {
    String str = s.toLowerCase();
    int l = 0, h = str.length() - 1;

    while (l < h) {
        char a = str.charAt(l);
        char b = str.charAt(h);
        if (Character.isLetterOrDigit(a) && Character.isLetterOrDigit(b)) {
            if (a != b) return false;
            l++;
            h--;
        } else if (!Character.isLetterOrDigit(a)) {
            l++;
        } else {
            h--;
        }
    }
    return true;
}

results matching ""

    No results matching ""