首页 > 其他 > 详细

7. Reverse Integer

时间:2019-04-30 19:13:52      阅读:155      评论:0      收藏:0      [点我收藏+]

注意翻转后不要超限:

Submission Detail

1032 / 1032 test cases passed.
Status: 

Accepted

Runtime: 40 ms
Memory Usage: 13.2 MB
Submitted: 0 minutes ago

Accepted Solutions Runtime Distribution

class Solution:
    def reverse(self, x: int) -> int:
        #define maxnum 2^32
        maxnum = 0x80000000
        if (x < -maxnum) or (x > (maxnum-1)) or x == 0:
            return 0
        
        #normal
        if (x<0):
            #min
            x = str(-x)
            x = x[::-1]
            x = int(x)  #deal pre-0
            if x < maxnum:      #returns 0 when the reversed integer overflows.
                return -x
            else:
                return 0
            
        else:
            #plus
            x = str(x)
            x = x[::-1]
            x = int(x)
            if x < maxnum-1:
                return x
            else:
                return 0

 

7. Reverse Integer

原文:https://www.cnblogs.com/alfredsun/p/10797643.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!