Unlocking LeetCode 7: Reverse Integer (A Beginner's Guide to Digital Flipping!)
Hello Devs! Today, we're diving into a classic LeetCode problem that's perfect for honing your logical thinking and handling tricky integer constraints: Problem 7: Reverse Integer. It sounds simple, but there's a neat catch!
Problem Explanation
We're given a signed 32-bit integer (that's a number that can be positive or negative, fitting within a specific range, roughly -2 billion to +2 billion). Our goal is to return this integer with its digits reversed.
Sounds easy, right? Here's the catch: If reversing the digits causes the number to go outside the valid 32-bit integer range, we must return 0. Also, we can't use 64-bit integers, which means we need to be careful with intermediate results if we were using a fixed-size integer language.
Let's look at some examples:
-
x = 123becomes321 -
x = -123becomes-321(the sign stays!) -
x = 120becomes21(leading zeros are dropped when reversing)
Intuition
The "aha!" moment for this problem often comes from realizing that dealing with individual digits of a number is much easier when it's not a number. Think of it: how do you reverse "123"? You literally flip the order. What data type is great for flipping characters? Strings!
So, our core idea is:
- Convert the integer to a string.
- Reverse the string.
- Convert it back to an integer.
- Perform the all-important range check.
Approach
Let's break down the steps:
- Handle the Sign: Before reversing, we need to know if the original number
xwas negative. Ifxis negative, we'll reverse its positive counterpart (e.g.,123from-123) and then apply the negative sign back at the end. - Convert and Reverse:
- If
xis negative, we first take its absolute value's string representation (str(x)[1:]effectively removes the-sign). Then, we reverse this substring using slicing ([::-1]). - If
xis positive, we simply convertxto a string (str(x)) and reverse it.
- If
- Convert Back to Integer: After reversing the string, we convert it back to an integer using
int(). - Apply Sign (if needed): If the original
xwas negative, multiply ourresby-1. - The Critical Overflow Check: This is where many solutions fail! We need to check if our
resis now outside the[-2^31, 2^31 - 1]range.- The maximum 32-bit signed integer is
2**31 - 1. - The minimum 32-bit signed integer is
-2**31. - If
resis too large or too small, return0. Otherwise, returnres.
- The maximum 32-bit signed integer is
Code
Here's the Python implementation based on our approach:
class Solution:
def reverse(self, x: int) -> int:
# Define the 32-bit integer range boundaries
MAX_INT = 2**31 - 1
MIN_INT = -2**31
res = 0
if x < 0:
# For negative numbers, remove the '-' sign, reverse, then re-apply '-'
# Example: -123 -> "123" -> "321" -> -321
res = int(str(x)[1:][::-1]) * -1
else:
# For positive numbers, simply convert to string, reverse, convert back
# Example: 123 -> "123" -> "321" -> 321
res = int(str(x)[::-1])
# Check for 32-bit integer overflow
if res > MAX_INT or res < MIN_INT:
return 0
return res
Time & Space Complexity Analysis
- Time Complexity: O(log |x|)
- The operations involve converting the integer to a string, reversing it, and converting it back. The number of digits in
xis proportional tolog10(|x|). All these string operations take time proportional to the number of digits.
- The operations involve converting the integer to a string, reversing it, and converting it back. The number of digits in
- Space Complexity: O(log |x|)
- We create a string representation of
x, which uses space proportional to the number of digits inx.
- We create a string representation of
Key Takeaways
- String Magic: For digit-based problems, converting integers to strings can often simplify reversal or digit manipulation.
- Edge Cases are Crucial: Always consider negative inputs and, most importantly, the integer overflow/underflow boundaries.
- Understand Data Types: Be aware of the maximum and minimum values your data types can hold, especially in competitive programming.
Happy coding!
Author Account: p1Hzd8mRM8
Time Published: 2026-05-17 12:06:31
Top comments (0)