5ms Java solution just one pass


  • 1
    S
    public boolean isPalindrome(String s) {
            if (s.length()<=1) return true;
    	int i = 0;
    	int j = s.length()-1;
    	char[] c = s.toCharArray();
    	while (j>i) {
    		char c1 = isVaild(c[i]);
    		if (c1-'0'<0) {i++; continue;}
    		char c2 = isVaild(c[j]);
    		if (c2-'0'<0) {j--; continue;}
    		if (c1 != c2) return false;
    		else {i++; j--;}
    	}
    	return true;
    }
    	
    private char isVaild(char c){
    	if (c-'a'>=0 && c-'a'<=26 || c-'0'>=0 && c-'0'<=9) return c;
    	else if (c-'A'>=0 && c-'A'<=26) return Character.toLowerCase(c);
    	else return '.';
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.