LeetCode – Reverse Integer:

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

**1. Naive Method**

We can convert the integer to a string/char array, reverse the order, and convert the string/char array back to an integer. However, this will require extra space for the string. It doesn’t seem to be the right way, if you come with such a solution.

**2. Efficient Approach**

Actually, this can be done by using the following code.

public int reverse(int x) { //flag marks if x is negative boolean flag = false; if (x < 0) { x = 0 - x; flag = true; } int res = 0; int p = x; while (p > 0) { int mod = p % 10; p = p / 10; res = res * 10 + mod; } if (flag) { res = 0 - res; } return res; } |

**3. Succinct Solution**

This solution is from Sherry, it is succinct and it is pretty.

public int reverse(int x) { int rev = 0; while(x != 0){ rev = rev*10 + x%10; x = x/10; } return rev; } |

**Handle Out of Range Problem**

As we form a new integer, it is possible that the number is out of range. We can use the following code to assign the newly formed integer. When it is out of range, throw an exception.

try{ result = ...; }catch(InputMismatchException exception){ System.out.println("This is not an integer"); } |

Please leave your comment if there is any better solutions.

can use recurse,

each step keep modulo,

on backtrack, *10+ current modulo

Java one line solution :)

`public static int reverse(int x) {`

StringBuilder sb = new StringBuilder();

return Integer.parseInt(sb.append(x).reverse().toString());

}

This solution enhances to the succinct solution which doesn’t account for math greater than the integer data types max value.

public int reverse(int x) {

long rev = 0;

while (x != 0) {

rev = rev * 10 + x % 10;

if ((x > 0 && rev > Integer.MAX_VALUE )|| (x < 0 && rev < Integer.MIN_VALUE)) {

return 0;

}

x /= 10;

`}`

return (int) rev;

}

public int reverse(int x) {

int n = Math.abs(x);

long result = 0;

while (n != 0) {

result = (result * 10) + (n % 10);

n = n / 10;

}

if (result > Integer.MAX_VALUE || result = 0 ? (int) result : (int) result * -1;

}

no,sorry underdtood 😀

i think there should be “or” between (max/ret-10)

if (ret != 0 && max/ret -10)

when ret==-1, could receive signal SIGFPE.

for example when x=-901000

This java code handles the overflow condition:

public int reverse(int x) {

long reverse = 0;

while( x != 0 ){

reverse = reverse * 10 + x % 10;

x = x/10;

}

if(reverse > Integer.MAX_VALUE || reverse < Integer.MIN_VALUE)

return 0;

else

return (int) reverse;

}

Thanks. would you explain in more detail? Thanks

But we can not use this method in python. Because in python -1 / 10 = -1 🙁

BufferOverflow already handeled by jason in a comment below.

This is not an accepted solution in CodeLeet. You should handle the buffer overflow case and return 0 when it happens. Please update the solution as follows :

public class Solution {

public int reverse(int x) {

int rev = 0;

while (x != 0) {

//Handel the buffer overflow : should be handeled before rev update

if (rev != 0 && Integer.MAX_VALUE / rev -10)

return 0;

rev = rev * 10 + x % 10;

x = x / 10;

}

return rev ;

}

}

Nice code!

Really nice code..can u please explain what u have done..

Here is my code, which can handle the overflow case.

int max = 1<<31;

int ret = 0;

for (; x != 0; x/=10) {

if (ret != 0 && max/ret -10)

return 0;

ret = ret*10 + x%10;

}

return ret;

Leading zero case would not exist in this solution since the input type is int.

For example, 1000000009 becomes 9000000001, which is more than the range of Integer in Java.

This code does not support the leading zero case too……

Nice code! And yes it supports negative numbers just fine.

I don’t think it is necessary to check whether the parameter is positive or negative. For instance, -11/10 = -1; -11%10 = -1. So this program could be even shorter:

public int reverse(int x) {

int rev = 0;

while(x != 0){

rev = rev*10 + x%10;

x = x/10;

}

return rev;

}

but basically they are the same.

In Java, I think you can get 0 if an integer is continuing divided by 10.

The solution is incorrect. In each loop, p needs to become the input with the last digit hacked off, so p should be adjusted by p = ( p – mod) / 10; That is, not *just* dividing by 10 — you have to subtract the remainder (mod) form dividing by ten, *then* divide by 10. Otherwise, just continuing to divide by 10 will never get you to 0 (think about it), and you’re in an infinite loop, hence the overflow. Here’s an example of the correct solution written in javascript: http://jsfiddle.net/mhfaust/XSNyr/

overflow? which case do you mean?

This will not handle the cases where the the reversed integer will cause overflow. This case should be handled.