Leetcode: Reverse Integer
1 min readDec 25, 2016
With help of Int to string conversion and string to int conversion we can solve this problem easily. We also need to check for integer overflow.
Remarks:
- O(n) space complexity where n is the length of integer
- O(n) time complexity for reversing the string of length n
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x >= 0:
val = int(str(x)[::-1])
else:
val = -1 * int(str(-x)[::-1]) return 0 if abs(val) > 2147483647 else val