Similar Problems

Similar Problems not available

Reverse Integer - Leetcode Solution

LeetCode:  Reverse Integer Leetcode Solution

Difficulty: Medium

Topics: math  

The "Reverse Integer" problem on LeetCode asks for a solution that takes an integer as an input and returns the reversed version of that integer. For example, if the input is 123, the output should be 321.

To solve this problem, we need to first understand a few things about how integer operations work in programming languages.

In most programming languages, integers are represented in binary form, which means they consist of a sequence of bits (0s and 1s). When we perform arithmetic operations on integers, we are actually manipulating these binary sequences.

One key fact about them is that we can "shift" the bits of an integer to the left or the right by a certain number of places, effectively multiplying or dividing the integer by a power of two.

For example, if we have the integer x = 123 (which is represented in binary as 01111011), we can shift its bits to the left by one place to get x * 2 = 246 (which is represented as 1111010). Similarly, we can shift its bits to the right by one place to get x // 2 = 61 (which is represented as 00111101).

With this knowledge, we can solve the "Reverse Integer" problem using the following steps:

  1. Check if the input integer is negative. If it is, we will need to flip its sign before reversing the digits.

  2. Initialize a variable called "reversed" to zero.

  3. While the input integer is not equal to zero, do the following:

    a. Get the rightmost digit of the input integer by taking the remainder of the division by 10. This gives us the digit in the ones place.

    b. Multiply the "reversed" variable by 10 to shift its digits to the left by one place.

    c. Add the rightmost digit from step 3a to the "reversed" variable.

    d. Divide the input integer by 10 (using integer division) to remove the rightmost digit.

  4. If the input integer was negative, multiply the "reversed" variable by -1 to flip its sign back.

  5. If the "reversed" variable overflows the range of a 32-bit integer, return 0 (which is the behavior specified by the problem statement).

  6. Otherwise, return the "reversed" variable.

Here is the Python code for this solution:

class Solution:
    def reverse(self, x: int) -> int:
        if x < 0:
            sign = -1
            x *= -1
            sign = 1
        reversed = 0
        while x != 0:
            digit = x % 10
            reversed = reversed * 10 + digit
            x //= 10
        reversed *= sign
        if reversed < -2**31 or reversed > 2**31 - 1:
            return 0
            return reversed

This code first checks if the input integer is negative and flips its sign if necessary. Then it initializes the "reversed" variable to zero and enters a loop that repeatedly extracts the rightmost digit of the input integer using the modulo operator (%), shifts the "reversed" variable to the left by multiplying it by 10, and adds the rightmost digit to the "reversed" variable. The loop continues until the input integer becomes zero.

After the loop, the code flips the sign of the "reversed" variable back if necessary and checks if it overflows the range of a 32-bit integer. If it does, the code returns 0; otherwise, it returns the "reversed" variable.

Reverse Integer Solution Code