Java solution (2ms beats 100%)


  • 0
    L
    public class Solution {
        public boolean isPalindrome(String s) {
            int start = 0, end = s.length() - 1;
            while (start < end) {
                char cs = s.charAt(start);
                if (!isAlphanum(cs)) {
                    start++;
                    continue;
                }
                char ce = s.charAt(end);
                if (!isAlphanum(ce)) {
                    end--;
                    continue;
                }
                if (cs == ce) {
                    start++;
                    end--;
                    continue;
                }
                if (cs > '9' && ce > '9') {
                    return Math.abs(cs - ce) == 32;
                }
                return false;
            }
            return true;
        }
        private boolean isAlphanum(char c) {
            return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ;
        }
    }
    

Log in to reply
 

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