給出一個 32 位的有符號整數,你須要將這個整數中每位上的數字進行反轉。spa
class Solution: def reverse(self, x: int) -> int: num = 0 #返回x的絕對值給a a = abs(x) while(a != 0): #123 #a=123 #num=0 #一、 #a=a/10 #num=a%10 num = num*10+(a%10) a = int(a/10) if x<0 and num<2147483647: return -num elif x>0 and num<=2147483647: return num else: return 0