LeetCode Verification transcript

我的方案:

class Solution {  
    public boolean isPalindrome(String s) { 
        boolean flag = true; 
        for(int i=0,j=s.length()-1;;) {
            while(i<s.length()/2&&!(Character.isLetterOrDigit(s.charAt(i))))
                i++;
            System.out.println(i);
            if(i>=s.length()/2) break;
            while(j>=s.length()/2&&!(Character.isLetterOrDigit(s.charAt(j))))
                j--;
            if(j<s.length()/2) break;
            char ch1=Character.isDigit(s.charAt(i))==true?s.charAt(i):Character.toLowerCase(s.charAt(i));
            char ch2=Character.isDigit(s.charAt(j))==true?s.charAt(j):Character.toLowerCase(s.charAt(j));
            //System.out.println(ch1);
            //System.out.println(ch2);
            if(ch1!=ch2) {
                flag = false; 
                break; 
            }
            i++;j--;
        }  
        return flag;
    }
}

The following code is the shortest time to submit a record:

public class Solution {
    private char validateChar(char ch) {
        if (ch >= 'A' && ch <= 'Z') {
            return (char) (ch + 'a' - 'A');
        } else if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
            return ch;
        } else {
            return 0;
        }
    }

    public boolean isPalindrome(String s) {
        char[] arr = s.toCharArray();
        for (int i = 0, j = arr.length - 1; i < j;) {
            char prev = validateChar(arr[i]);
            if (prev == 0) {
                i++;
                continue;
            }
            char end = validateChar(arr[j]);
            if (end == 0) {
                --j;
                continue;
            }
            if (prev != end) {
                return false;
            }
            ++i;
            --j;
        }
        return true;
    }
}