呓语Beta 2.0 首页 镜头后 灶台前 捐赠者名单 有趣的小站

leetcode 7 Reverse Integer

文档信息

leetcode 657 Judge Route Circle

解法一

class Solution(object):
    def reverse(self, x):
    """
    :type x: int
    :rtype: int
    """
    negative = False
    if (x < 0):
        negative = True
        x = abs(x)
                                                                                       
    reversed_x = int(''.join(list(reversed(list(str(x))))))
                                                                                                       
    if negative:
        reversed_x *= (-1)
                                                           
    return 0 if reversed_x < (-1) * (2 ** (32-1)-1) or reversed_x > 2 ** (32-1) else reversed_x

note

解法二

def reverse(self, x):
    s = cmp(x, 0)
    r = int(`s*x`[::-1])
    return s*r * (r < 2**31)

note

捐赠支持

可以使用支付宝转帐给richard.ma.19850509#gmail.com(请将#替换为@),谢谢!

Fork me on GitHub