A Java solution, beats 89.67%,please comment to improve it.


  • 0
    A

    public boolean isPalindrome(int x) {
    if(0 > x) return false;
    int len = getL(x);
    while(x > 9)
    {
    int head = x/len;
    int tail = x%10;
    if (head != tail) return false;
    x = (x%len)/10;
    len /= 100;
    if(len > 0 && x > 0)
    {
    int newl = getL(x);
    if(x%(len/newl) != 0) return false;
    }
    }
    return true;
    }
    public int getL(int x)
    {
    int len = 1;
    if(x <= 0) return len;
    while(x/len >= 10)
    {
    len = len*10;
    }
    return len;
    }


Log in to reply
 

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