這個問題是個相當(dāng)簡單的問題喊儡,描述如下
Reverse digits of an integer.
Example1: x = 123, return 321Example2: x = -123, return -321
**Note:
**The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
最開始想思路的時候士骤,也是想著判斷多少位辩越,然后就發(fā)現(xiàn),其實將整數(shù)記錄正負姑裂,然后變成自然數(shù)馋袜,最后轉(zhuǎn)成字符串,將字符串倒序排列炭分,然后再轉(zhuǎn)回整形桃焕,最后將正負加回來就好了,好了捧毛,就直接接代碼了
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
#最開始的時候不知道32-bit怎么算,后來想起來了让网,python中并沒有記錄這個數(shù)呀忧,所以只能自己計算了
max_32 = 2**31-1
if x>max_32 or x < - max_32:return 0
array = []
zi_ran = 1 if x >= 0 else -1
x = x if x >= 0 else -x
string = '%ld'%(x)
str = string[::-1]
x = int(str)
if x > max_32 or x < - max_32: return 0
x = x * zi_ran
return x