Java solution,fast(8ms) and easy to understand


  • 0
    P
    public boolean isPalindrome(String s) {
    	if (s.length() == 1) {
    		return true;
    	}
    	char[] array = s.toCharArray();
    	int i = 0, j = array.length - 1;
    	char startChar, endChar;
    	while (i < j) {
    		while (!Character.isLetterOrDigit(array[i]) && i < j) {
    			i++;
    		}
    		while (!Character.isLetterOrDigit(array[j]) && i < j) {
    			j--;
    		}
    		startChar = Character.toLowerCase(array[i]);
    		endChar = Character.toLowerCase(array[j]);
    		if (startChar != endChar) {
    			return false;
    		}
    		i++;
    		j--;
    	}
    	return i >= j;
    }

Log in to reply
 

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