My fast Java solution(5ms)


  • 1
    K

    this is my code

    public boolean isPalindrome(String s) {
            int i = 0, j = s.length() - 1;
            while (true) {
                if (i >= j) return true;
                char a = toLowerCase(s.charAt(i));
                if (a == ' ') {
                    i++;
                    continue;
                }
                char b = toLowerCase(s.charAt(j));
                if (b == ' ') {
                    j--;
                    continue;
                }
                if (a != b)
                    return false;
                i++;
                j--;
            }
        }
    
        public char toLowerCase(char c) {
            if ('A' <= c && c <= 'Z') return (char) (c + 32);
            if ('0' <= c && c <= '9') return c;
            if ('a' <= c && c <= 'z') return c;
            return ' ';
        }
    

Log in to reply
 

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