题目:
没有什么算法思想,按照题目要求,能将32位数以内的带符号的整数反转就行了,实现代码如下:
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if -10 < x < 10:
return x
str_x = str(x)
if str_x[0] != "-":
str_x = str_x[::-1]
x = int(str_x)
else:
str_x = str_x[:0:-1]
x = int(str_x)
x = -x
return x if -2147483648 < x < 2147483647 else 0
提交结果: