In the Leetcode Reverse Integer problem solution in Python programming Given 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 [-231, 231 – 1], then return 0.
Leetcode Reverse Integer problem solution in Python programming
class Solution(object):
def reverse(self, x):
ans = int(str(x)[::-1]) if x > 0 else -int(str(x).replace('-', '')[::-1])
return 0 if ans < -2**31 or ans > 2**31 - 1 else ans
Also read,
- Leetcode Reverse Integer problem solution in C
- Leetcode Reverse Integer problem solution in Java
- Leetcode Reverse Integer problem solution in C++
- Leetcode Reverse Integer problem solution in C#