class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x == 0:
return 0
flag = 0
if x < 0:
x = str(-x)
flag = 1
else:
x = str(x)
x = x[::-1]
for i in x:
if i == "0":
x = x.replace("0", "", 1) # replace函數的使用
else:
break
if flag == 0:
x = int(x)
else:
x = int("-" + x)
if -pow(2,31) <= x <= pow(2,31) - 1: # pow函數的使用
return x
else:
return 0
print(Solution.reverse(Solution(), -200))
python實現整數反轉
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.