Leetcode Reverse Integer problem solution in Python programming

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,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *