### 7. Reverse Integer

#### Problem:

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

**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?

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

#### Solution 1

The basis of integer dealing.

```
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
flag = 1 if x > 0 else -1
x = abs(x)
integer = 0
while x != 0:
last = x % 10
integer = integer * 10 + last
x /= 10
integer *= flag
if -2 ** 31 <= integer <= 2 ** 31 - 1:
return integer
return 0
```

#### Solution 2

Trick, but no need.

```
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
s = str(x)
res = int('-' + s[1:][::-1]) if s[0] == '-' else int(s[::-1])
return res if -2147483648 <= res <= 2147483647 else 0
```