zoukankan      html  css  js  c++  java
  • 7. Reverse Integer

    Given a 32-bit signed integer, reverse digits of an integer.

    Note:
    Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2**31,  2**31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

     
    class Solution(object):
        def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            if (x > 2147483647 and x < -2147483648):
                return 0
            
            x_list = list(str(x))
            
            if x_list[0] == "-":
                x_list = x_list[1:]
                x_list.reverse()
                
                if (int(''.join(x_list)) > 2147483647):
                    return 0
    
                return -int(''.join(x_list))
            else:
                x_list.reverse()
                
                if (int(''.join(x_list)) > 2147483647):
                    return 0
                
                return int(''.join(x_list))
            
                    
                    
  • 相关阅读:
    python笔记目录
    Django 的View(视图)系统
    051_Bootstrap 框架
    050_jQuery 事件
    049_jQuery 操作标签
    048_jQuery
    016-递归函数
    047_BOM_DOM
    046_JS
    045_CSS
  • 原文地址:https://www.cnblogs.com/jyg694234697/p/9531719.html
Copyright © 2011-2022 走看看