正數反轉

def reverse(x):
    a = x if x >= 0 else -x
    li = []
    while a:
        b = a % 10
        li.append(b)
        a = int(a / 10)
    c = 0
    for i in li:
        c = c*10 + i
    c = c if x >= 0 else -c
    return c if c < 2147483647 and c > -2147483648 else 0
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        x = int(str(x)[::-1]) if x>=0 else -int(str(-x)[::-1])
        return x if x < 2147483647 and x > -2147483648 else 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章