8 lines of java accepted solution, quite concise

  • 1
        if (x < 0)  return false;
        int parse = x;
        int reverse = 0;
        while (parse > 0) {
            reverse = reverse * 10 + parse % 10;
            parse = parse /10;
        return reverse == x;

  • 0

    this has issues with overflow, but the overflow problem only occurs with non-palindrome input x, so the end result got lucky and returned false too.

  • 0

    Can you give me an example under what situation will the overflow occurs? I got a bit confused about the overflow problems.

  • 0

    @teddyyyy is right, a simple example: if x = INT_MAX(2147483647), your algorithm just reverse x to 7463847412, which will overflow exactly, but the reason you got an AC is that this overflow positive integer 7463847412 may be saved as an another integer (negetive or something different), which can not be equal to x(INT_MAX) in the memory.

Log in to reply

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