【题目】
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
【分析】
水题,没啥好说的,这题见过好多次了...
【代码】
class Solution:
# @return an integer
def reverse(self, x):
if x < 0:
x = -x
result = 0
while x > 0:
remainder = x % 10
result = result * 10 + remainder
x = int(x / 10)
return -result
else:
result = 0
while x > 0:
remainder = x % 10
result = result * 10 + remainder
x = int(x / 10)
return result