LeetCode – Reverse Integer

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.

24 thoughts on “LeetCode – Reverse Integer”


  1. Java one line solution :)

    public static int reverse(int x) {
    StringBuilder sb = new StringBuilder();
    return Integer.parseInt(sb.append(x).reverse().toString());
    }

  2. 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;
    }

  3. 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;
    }

  4. if (ret != 0 && max/ret -10)
    when ret==-1, could receive signal SIGFPE.
    for example when x=-901000

  5. 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;

    }

  6. 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 ;
    }
    }

  7. 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;

  8. 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.

  9. 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/

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

Leave a Comment