7. Reverse Integer

Understanding and Implementing the Reverse Integer Problem

In the world of algorithms and coding challenges, the reverse integer problem is a classic one. Whether you're preparing for coding interviews or just looking to improve your problem-solving skills, this problem offers a good mix of logic, string manipulation, and handling edge cases. Let's dive into understanding and implementing the solution for this problem.

The Problem Statement

Given a 32-bit signed integer, you need to reverse the digits of the integer. For example, if the input is 123, the output should be 321. If the input is -123, the output should be -321. Additionally, you must handle cases where the reversed integer might overflow beyond the 32-bit signed integer range.

Intuition

The core intuition behind solving this problem is straightforward:

  1. Convert the integer to its string representation.

  2. Reverse the string.

  3. Convert the reversed string back to an integer.

  4. Restore the sign if necessary.

  5. Ensure the result is within the 32-bit signed integer range.

Approach

  1. Edge Case: Check if the input integer is zero. If yes, return zero immediately.

  2. Constants Definition: Define the maximum and minimum 32-bit signed integer values to handle overflow.

  3. String Manipulation:

    • If the integer is positive, reverse its string representation.

    • If the integer is negative, strip the sign, reverse the string, and then reapply the sign.

  4. Overflow Check: After reversing, check if the integer exceeds the 32-bit range. If it does, return zero.

  5. Return the Result: Return the reversed integer if it falls within the valid range.

Code Implementation

Here’s the code implementation that encapsulates the above approach:

javascript
var reverse = function(x) {
    if(x === 0) {
        return 0;
    }
    const MAX_VALUE = 2147483647; // Maximum 32-bit integer value
    const MIN_VALUE = -2147483648; // Minimum 32-bit integer value
    let reverse = 0;
    if(x > 0) {
        reverse = parseInt(String(x).split('').reverse().join(''));
    } else {
        reverse = parseInt(String(x).slice(1).split('').reverse().join('')) * -1;
    }
    if(reverse > MAX_VALUE || reverse < MIN_VALUE) {
        return 0;
    }
    return reverse;
};

Complexity Analysis

  • Time Complexity: O(N), where N is the number of digits in the integer. Converting the integer to a string, reversing it, and converting it back to an integer all take linear time.

  • Space Complexity: O(N), where N is the number of digits in the integer. This is due to the space required to store the string representation of the integer.

Conclusion

The reverse integer problem is a great exercise for understanding basic string manipulations and handling edge cases in coding. By keeping the solution clean and efficient, and by ensuring proper handling of potential overflows, you can solve this problem effectively. Remember, the key lies in breaking down the problem into smaller steps and addressing each part methodically.

Happy coding!

LeetCode Solution Link: https://leetcode.com/problems/reverse-integer/solutions/6178690/easy-to-understand-solution-by-bikash_ku-cg7a/

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

14. Longest Common Prefix