【Leetcode】【python】Reverse Integer

来源:互联网 发布:node安装教程 编辑:程序博客网 时间:2024/05/16 04:31

题目大意

反转整数123变为321,-123变为-321

注意:在32位整数范围内,并且001要成为1

解题思路

题目简单,各种语言特性不同,各显神通了。

代码

class Solution(object):    def reverse(self, x):        """        :type x: int        :rtype: int        """        if x < 0:            result = -int(str(-x)[::-1])  # 字符串倒序输出        else:            result = int(str(x)[::-1])        if result < -2147483648 or result > 2147483647:            return 0        return result

总结