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
查看更多关于leetcode——7.整数反转的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did170667