题解 | #反转数字#
反转数字
http://www.nowcoder.com/practice/1a3de8b83d12437aa05694b90e02f47a
#
#
# @param x int整型
# @return int整型
#
class Solution:
def reverse(self , x ):
# write code here
n = 0
flag = x>0
x = abs(x)
t = x
while t!=0:
# a = x/10
t = t/10
n += 1
t = x
res = 0
while t!=0:
res += (t%10) * (10**(n-1))
t = t/10
n -= 1
if not flag:
res = (-1)*res
if res > 2**31-1 or res < -1 * (2**31):
return 0
return res
查看9道真题和解析
