#7
Reverse Integer
medium· Bit Manipulationruns: 0Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], return 0. Assume the environment does not allow you to store 64-bit integers.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 376
class Solution:
def reverse(self, x: int) -> int:
INT_MAX = 2**31 - 1
INT_MIN = -(2**31)
sign = -1 if x < 0 else 1
x = abs(x)
result = 0
while x:
result = result * 10 + x % 10
x //= 10
result *= sign
if result > INT_MAX or result < INT_MIN:
return 0
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.