LeetCode1.2

作者: supermanwasd | 来源:发表于2019-01-02 21:35 被阅读0次

    Reverse Integer

    Screen Shot 2019-01-02 at 9.31.09 PM.png

    今天这个题官方认为很简单哈。
    答案:

        class Solution:
        def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            b = 2 ** 31
            neg_b = -1 * b 
        
            rev = 0 
        
            if x > 0:
                while x != 0:
                    digit = x % 10 
                    x //= 10 
                    rev = rev * 10 + digit 
            else:
                x = abs(x)
                while x != 0:
                    digit = x % 10 
                    x //= 10 
                    rev = rev * 10 + digit
                rev *= -1
        
            if rev > b or rev < neg_b:
                return 0 
            return rev 

    相关文章

      网友评论

        本文标题:LeetCode1.2

        本文链接:https://www.haomeiwen.com/subject/cyitrqtx.html