7. Reverse Integer
https://leetcode.com/problems/reverse-integer/
Python
Forces Bust
class Solution:
def reverse(self, x: int) -> int:
digits = str(x)[::-1]
if digits[-1] == '-':
result = -int(digits[:-1])
else:
result = int(digits)
return result if result > 1-2**31 and result < 2**31-1 else 0
Queue
from collections import deque
class Solution:
def reverse(self, x: int) -> int:
queue = deque()
# Check is native or not
if x < 0:
is_native = True
x = abs(x)
else:
is_native = False
# Push to queue
while x // 10 > 0:
queue.append(x % 10)
x //= 10
queue.append(x)
# Pop from queue and accumulate to the reversed num
power = len(queue) - 1
result = 0
while queue:
result += queue.popleft() * (10 ** power)
power -= 1
if is_native:
result = -result
return result if result > 1-2**31 and result < 2**31-1 else 0