7. Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

要保证输出的数字是int32类型,利用int和str类型的转换解决问题

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x>=0:
            result = int(str(x)[::-1])
        else:
            result = int(str(abs(x))[::-1]) * (-1)
        
        if not -2**31 < abs(result) < 2**31 - 1:
            result = 0
        
        return result

  

posted @ 2018-12-29 00:53  wuxinyi  阅读(105)  评论(0编辑  收藏  举报