Java beats 90%, check only if you don't understand other solutions


  • 1

    Idea is to get the i'th digit from the beginning and i'th digit from the end. For beginning you cannot remove the digits prior to that because if the next digit is 0 and if you remove the digit 0's will be removed too. eg.
    x = 100021
    if you remove first one and last 1 then the number
    x = 2 (0's will not count)

    public boolean isPalindrome(int x) {
            if(x<0) return false;
            int digits=0, temp=x;
            while(temp>0){
                digits++;
                temp/=10;
            }
            while(digits>1){
                int a = (x/(int) Math.pow(10,digits-1))%10;
                int b = x%10;
                if(a!=b) return false;
                x = x / 10;
                digits -= 2;
            }
            return true;
        }
    

Log in to reply
 

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