Leetcode-07 Reverse Integer
题目:
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
实现:
class Solution:
def reverse(self, x):
res = 0
flag = 1 if x > 0 else -1
x = abs(x)
while x > 0:
res = res * 10 + x % 10
x /= 10
return flag*res