7. reverse text

Gives a 32-bit signed integer, you need this integer number on each inverted.

Input: 123 
Output: 321
Input: -123 
Output: -321
Input: 120 
Output: 21

note:

Suppose we have an environment can only store 32-bit signed integer, please Under this assumption, if the reverse integer overflow it returns 0. Values ​​in the range [-2 ^ 31, 2 ^ 31 - 1]

And topics like yesterday, right? However, there are several different places, see the second example, enter 123, the required output -321, indicating that the reversal was not signed.

 There is a special condition that the value range of [-2 ^ 31, 2 ^ 31--1], that is, when the output value is also necessary to determine the size.

# -*-coding utf-8-*-
def reverse(x):
num = 0
a = abs(x)
while a != 0:
temp = a % 10
num = num * 10 + temp
a = a // 10

if x > 0 and num < (2 ^ 31 - 1):
return num
elif x < 0 and num > (-2 ^ 31):
return -num
else:
return 0


rd = reverse(int(-123))
print(rd)

Guess you like

Origin www.cnblogs.com/xqy-yz/p/11411194.html