1呻引、題目
給你一個(gè) 32 位的有符號(hào)整數(shù) x ,返回將 x 中的數(shù)字部分反轉(zhuǎn)后的結(jié)果偏塞。
如果反轉(zhuǎn)后整數(shù)超過(guò) 32 位的有符號(hào)整數(shù)的范圍 [?231, 231 ? 1] 鉴逞,就返回 0睬辐。
假設(shè)環(huán)境不允許存儲(chǔ) 64 位整數(shù)(有符號(hào)或無(wú)符號(hào))。
2、代碼
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
res=0;is_flag=0
if x<0:
x=0-x;is_flag=1
while x:
a=x%10
res=res*10+a
x=x//10
if is_flag==1:
res=0-res
return res
3、用例
s=Solution()
ts=-562
res=s.reverse(ts)
print(res)