Beat 90%+ in JAVA

  • 10
     public boolean isPalindrome(int x) {
            if (x < 0) return false;
            int ans = 0;
            int num = x;
            while ( num > 0) {
                ans = ans * 10 + num % 10;
                num = num / 10;
            if (ans != x) return false;
            return true;

  • 0

    Good answer! Its like reverse the Integer

  • 0

    Is Integer ans & num using extra space?

  • 5

    There is a problem with your solution.The max value of Integer is 2147483647,if you get this as input,the number wil overflow.

  • 0

    In fact an int has reversed number overflow, it means its reverse is not an int, so it does not count as a palindrome number in the this sense. And if overflow occurs, ans will almost certainly equal to x, that is probably why his/her code could pass the test. But in reality I think an error should be thrown anyway.

  • 1

    return ans == x;

  • 0

    I always thought about comparing the two digits which are the first and the last numbers. This is quite a clear answer.

Log in to reply

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