Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
用的是Python 转换成str ,然后翻转,需要注意
1.翻转之后如果溢出,输出0
class Solution: def reverse(self, x):
"""
:type x: int
:rtype: int
"""
neg = x<0
res = int(''.join(str(abs(x))[::-1]))
if res > 2**31:
res = 0
if neg:
res = -res
return res