题目

Reverse digits of an integer.

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

Python

1
2
3
class Solution:
def reverse(self, x):
pass

C

1
2
3
4
5
6
7
8
9
10
11
12
int reverse(int x)
{
int r = 0;
while (x != 0)
{
if (r > INT_MAX/10 || r < INT_MIN/10)
return 0;
r = r*10 + x%10;
x /= 10;
}
return r;
}