7.翻转整数

输入:123

输出:321

输入:-123

输出:-321

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def reverse_integer(i):
r_i = 0
is_negative = False

if i < 0:
is_negative = True i = -i

while i > 0:
r_i = r_i * 10 + i % 10
i = int(i/10)

if is_negative:
return -r_i
return r_i