1
1

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

asked 26 Dec '11, 04:36

1337c0d3r's gravatar image

1337c0d3r ♦♦
1.0k567167
accept rate: 2%


12next »

int reverse(int x) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int lastDigit = 0;
    int result = 0;
    bool isNeg = x>0? false:true;
    x = abs(x);

    while(x>0)
    {
        lastDigit = x%10;
        result = result*10 + lastDigit;
        x = x/10;
    }

    if(result<0) return -1;

    if(isNeg)
        result *=-1;

    return result;        
}
link

answered 31 Dec '12, 15:49

codingtmd's gravatar image

codingtmd
361
accept rate: 0%

public int reverse(int x){

    // Start typing your Java solution below
    // DO NOT write main() function

    int r = 0;

    while(x != 0) {
        r = r*10 + x % 10;
        x /= 10;
    }
    return r;
}
link

answered 14 Jan, 22:08

splax's gravatar image

splax
112
accept rate: 0%

edited 15 Jan, 07:29

class Solution {
public:
int reverse(int x) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
        bool zheng = (x > 0) ? true : false;
int temp = abs(x);
int res = 0;
while (temp > 0)
{
    res = res*10 + temp%10;
    temp /= 10;
}
if(zheng)
    return res;
else
    return -1*res;
}
};
link
This answer is marked "community wiki".

answered 15 Jan, 02:09

BlackMamba's gravatar image

BlackMamba
10124
accept rate: 0%

class Solution { public: int reverse(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function

    int absx;
    int modx=1, leftx=11; 
    int sum=0;

    if(x<0) absx=-1*x; 
    if(x>0) absx=x; 
    if(x==0) return x;

    while(modx>0){
        modx=absx/10; 
        leftx=absx-10*modx;
        absx=modx; 
        sum=sum*10+leftx; }

    if(x<0) return -sum; 
    else return sum;

}

};

link

answered 01 Feb, 13:33

cpp's gravatar image

cpp
103
accept rate: 0%

class Solution { public:

int reverse(int x)

{ int r=0;

while(x!=0) {

r=r*10+x%10; x=x/10; } return r; } };

link

answered 03 Feb, 11:01

khwaja's gravatar image

khwaja
112
accept rate: 0%

edited 03 Feb, 11:02

//my code shows the history how to reduce LOC
int reverse(int x) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int result =0;
    //int remainder;
    //bool boolX = (x>0?true:false);

    //if(boolX == false) x = -1 *x;

    while(x !=0 ){
        //cout << "x: " << x << endl; 
        result = result *10 + x % 10;
        //remainder = x % 10;
        //result += x % 10;
        x = x /10;

        //cout << "result: " << result << endl;

    }

    //result += x;

    //if(boolX == false) return -1 *result; 
    return result;

}
link

answered 21 Feb, 17:46

27606's gravatar image

27606
83
accept rate: 0%

public int reverse(int x) {
    if(x==0)
        return 0;

    StringBuilder sb = new StringBuilder();

    if(x<0)
        sb.append("-");

    x=Math.abs(x);
    while(x>0){
        sb.append(x%10);
        x=x/10;
    }

    return Integer.parseInt(sb.toString());
}
link

answered 09 May, 08:23

Tanu's gravatar image

Tanu
212
accept rate: 0%

int reverse(int x) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    stack<int> s;
    int p = 1;

    if (x < 0) {
        p = -1;
        x = x * p;
    }

    while (x > 0) {
        int digit = x % 10;
        s.push(digit);
        x = x / 10;
    }

    int r = 0, i = 0;
    while (!s.empty()) {
        int digit = s.top();
        s.pop();
        r += digit * pow(10, i);
        i++;
    }

    return r*p;
}
link

answered 11 May, 00:38

whitebluff's gravatar image

whitebluff
212
accept rate: 0%

public class Solution {

public int reverse(int x) {
    int neg = (x<0)?-1:1;
    x *= neg;

    int cnt = 10, rev = 0;
    while(x>=cnt/10){
        rev = rev*10 + (x%cnt - x%(cnt/10)) / (cnt/10);
        cnt *= 10;
    }

    return rev*neg;
}

}

link

answered 06 Jun, 07:49

lfgao's gravatar image

lfgao
1
accept rate: 0%

        int reverse(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function

        int rem = x;
        int result = 0;

        while (rem != 0)
        {
            result = result * 10 + rem % 10;
            rem /= 10;            
        }

        return result;
    }
link

answered 25 Jun, 20:27

Bill's gravatar image

Bill
111
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • Indent code by 4 spaces.
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×136

Asked: 26 Dec '11, 04:36

Seen: 2,391 times

Last updated: 10 Sep, 00:16