LeetCode1.2
2019-01-02 本文已影响0人
supermanwasd
Reverse Integer
Screen Shot 2019-01-02 at 9.31.09 PM.png今天这个题官方认为很简单哈。
答案:
class Solution:
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
b = 2 ** 31
neg_b = -1 * b
rev = 0
if x > 0:
while x != 0:
digit = x % 10
x //= 10
rev = rev * 10 + digit
else:
x = abs(x)
while x != 0:
digit = x % 10
x //= 10
rev = rev * 10 + digit
rev *= -1
if rev > b or rev < neg_b:
return 0
return rev