zoukankan      html  css  js  c++  java
  • leetcode——7.整数反转

    class Solution:
        def reverse(self, x: int) -> int:
            i=1
            if x==0:
                return 0
            while x%(10**i)==0:
                i+=1    
            if x>=0:
                a=list(str(x))
                b=[int(a[i])*10**i for i in range(len(a))]
                if sum(b)>2**31-1:
                    return 0
                return sum(b)
            elif x<0:
                a=abs(x)
                a=list(str(a))
                b=[int(a[i])*10**i for i in range(len(a))]
                if -sum(b)<-2**31:
                    return 0
                return -sum(b)
    执行用时 :44 ms, 在所有 Python3 提交中击败了90.44%的用户
    内存消耗 :13.8 MB, 在所有 Python3 提交中击败了5.21%的用户
     
    执行用时为 20 ms 的范例
    class Solution:
        def reverse(self, x: int) -> int:
            rea=int(str(x)[::-1]) if x>=0 else -int(str(-x)[::-1])
            return rea if rea.bit_length()<32 else 0
    执行用时 :36 ms, 在所有 Python3 提交中击败了99.18%的用户
    内存消耗 :14 MB, 在所有 Python3 提交中击败了5.21%的用户
     
    精巧!!
    return rea if rea.bit_length()<32 else 0
    精巧!!!!
                                                                         ——2019.10.10
     
     
     
     
     
     
    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    通过使用 SQL,可以为列名称和表名称指定别名(Alias)
    BETWEEN 操作符
    IN 操作符
    SQL 通配符
    LIKE 操作符
    TOP 子句
    DELETE 语句
    Update 语句
    INSERT INTO 语句
    IOS SWIFT 网络请求JSON解析 基础一
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11648730.html
Copyright © 2011-2022 走看看