Valid Palindrome

题目:

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

分析:

人家答案里面用的判断方式是

return Character.isLetter(c) || Character.isDigit(c);

解法:

public class Solution {
    /**
     * @param s A string
     * @return Whether the string is a valid palindrome
     */
    public boolean isChar(char character) {
        return ( ('0' <= character && character <= '9') || ('a' <= character && character <= 'z') || ('A' <= character && character <= 'Z') );
    } 

    public boolean isPalindrome(String s) {
        int i = 0;
        int j = s.length() - 1;

        while (i < j) {

            while (i < j && !isChar(s.charAt(i) ) ) {
                i++;
            }
            while (i < j && !isChar(s.charAt(j) ) ) {
                j--;
            }

            if (Character.toLowerCase(s.charAt(i) ) == Character.toLowerCase(s.charAt(j) ) ) {
                i++;
                j--;
            } else {
                return false;
            }

        }
        return true;
    }
}

results matching ""

    No results matching ""