# LeetCode:Reverse Integer

### Reverse Integer

Total Accepted: 108334 Total Submissions: 462300 Difficulty: Easy

Reverse digits of an integer.

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

click to show spoilers.

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?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10):

Subscribe to see which companies asked this question

code:

class Solution {
public:
int reverse(int x) {
if(0 == x) return 0;
int ret;
long long sum = 0;
while(0 == x % 10) x /= 10;
while(x) {
sum = sum * 10 + x % 10;
x /= 10;
}
ret = sum;
if(ret == sum) return ret;
else return 0;
}
};

01-26 3013

02-28 3139

09-20 1290

07-11 1094

02-18 878

05-02 1759

03-22 1045

06-06 1582

07-18 4124

04-20 5278