zoukankan      html  css  js  c++  java
  • 7. Reverse Integer(翻转整数)



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

    Example 1:

    Input: 123
    Output:  321
    

    Example 2:

    Input: -123
    Output: -321
    

    Example 3:

    Input: 120
    Output: 21
    

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

    用的是Python 转换成str ,然后翻转,需要注意

    1.翻转之后如果溢出,输出0

     1 class Solution:
     2 
     3     def reverse(self, x):
     4         """
     5         :type x: int
     6         :rtype: int
     7         """
     8         neg = x<0
     9         res = int(''.join(str(abs(x))[::-1]))
    10         if res > 2**31:
    11             res = 0
    12         if neg:
    13             res = -res
    14         return res
  • 相关阅读:
    Android AdapterView View的复用机制 分析
    go12---interface
    go11---方法method
    go10---struct
    go09---defer
    go8---函数function
    go7---map
    go6---slice切片
    go5--数组
    go4--break,continue + 标签
  • 原文地址:https://www.cnblogs.com/zle1992/p/8640282.html
Copyright © 2011-2022 走看看