当前位置: 移动技术网 > IT编程>脚本编程>Python > Leetcode解题 7. Reverse Integer 反转整数

Leetcode解题 7. Reverse Integer 反转整数

2018年08月12日  | 移动技术网IT编程  | 我要评论

快乐大本营20120317,诙新闻,过期的爱情

 

没看清要求,提交错误一次。

要求是 "如果反转后的整数溢出,则返回 0"。

 

class Solution(object):
    def reverse(self, x):
        if x < 0:
            y = int(str(x)[0:1] + str(x)[:0:-1])
            if y < -2 ** 31:
                return 0
            else:
                return y
        else:
            y = int(str(x)[::-1])
            if y > 2 ** 31 - 1:
                return 0
            else:
                return y

Solution = Solution()
print Solution.reverse(1534236469)

  

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网