Given a 32-bit signed integer, reverse digits of an integer.
将数值从后往前读取,负号保留不动
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
class Solution(object): def reverse(self, x): """ :type x: int :rtype: int """ num = 0 abs_x = abs(x) while abs_x != 0: tmp = abs_x % 10 num = num * 10 + tmp abs_x = abs_x // 10 if x > 0 and num < 2 ** 31 - 1: return num elif x < 0 and num < 2 ** 31: return -num else: return 0