有符号的整数翻转

示例1:

输入:234 输出:432

示例2:

输入:-142 输出: -241

示例3:

输入:120 输出:21

 

Python解决方案:

将整形转换成字符串:

    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        if not x:
            return x
        s = str(x)
        if s[0] in ["-","+"]:
            if s[-1] == 0:
                s = s[0] + s[1:][::-1][1:]
            else:
                s = s[0] + s[1:][::-1]
        else:
            s = s[::-1]
        s = int(s)
        return s

 

posted @ 2019-03-15 10:17  秦qin  阅读(210)  评论(0编辑  收藏  举报