NLeetCode Solutions
ProblemsPatternsStatistics
© 2026 Created by Trịnh Minh Nhật
GitHubLeetCode
Related posts
  • 9. Palindrome NumberEasy
  • 13. Roman to IntegerEasy
  • 70. Climbing StairsEasy
  • 509. Fibonacci NumberEasy

7. Reverse Integer

Medium
Math
Companies:
AppleNetEaseBloomberg
Answered: Dec 5, 2022
View on LeetCode

Problem Description

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.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Examples

Example 1

Input: x = 123
Output: 321

Example 2

Input: x = -123
Output: -321

Example 3

Input: x = 120
Output: 21

Constraints

  • -231 <= x <= 231 - 1

Solutions

Complexity Analysis

Time Complexity:O(logx)

The number of digits in x is proportional to log10(x), so converting to a string, reversing it, and converting back each take O(log⁡x)O(\log x)O(logx) time.

Space Complexity:O(logx)

We create a string representation of x, which requires O(log⁡x)O(\log x)O(logx) space to store its digits.

This approach leverages string slicing to reverse the digits without manual digit extraction. We record the sign of x, convert abs(x) to a string, reverse it with [::-1], convert it back to an integer, and reapply the sign. Finally, we check whether the result falls outside the 32-bit signed integer range [-231, 231 - 1] and return 0 if it does.

Complexity Analysis

Time Complexity:O(logx)

The while loop iterates once per digit of x, and the number of digits is proportional to log10(x).

Space Complexity:O(1)

We only use a constant number of variables (sign, reversed_x, digit) regardless of the input size.

This approach also builds the reversed number digit by digit using the modulo operator, but performs the overflow check after each digit is appended to reversed_x. In each iteration, we extract the last digit of x, update reversed_x = reversed_x * 10 + digit, and then check whether reversed_x has exceeded the 32-bit signed integer bounds — returning 0 if it has. Finally, we reapply the original sign before returning. This is simpler than the pre-check approach but relies on the language supporting arbitrarily large integers during intermediate computation.

Complexity Analysis

Time Complexity:O(logx)

The while loop iterates once per digit of x, and the number of digits is proportional to log10(x).

Space Complexity:O(1)

We only use a constant number of variables (sign, reversed_x, digit, MAX_INT, MIN_INT) regardless of the input size.

This approach builds the reversed number digit by digit using the modulo operator, and critically, checks for potential 32-bit overflow before appending each digit to reversed_x. In each iteration, we extract the last digit of x with x % 10, then verify that multiplying reversed_x by 10 and adding the digit would not exceed MAX_INT or go below MIN_INT — returning 0 immediately if it would. This pre-check ensures we never compute a value outside the 32-bit range, satisfying the problem's constraint of not using 64-bit integers.

Conclusion

ApproachRatingTime ComplexitySpace ComplexityAdvantagesDisadvantages
String Reversal
O(log⁡x)O(\log x)O(logx)O(log⁡x)O(\log x)O(logx)Simple and readableUses extra space to store the string representation
Math with Post-overflow Check
O(log⁡x)O(\log x)O(logx)O(1)O(1)O(1)Simpler digit-by-digit logic than the pre-check approachRelies on the language supporting arbitrary-precision integers during intermediate computation
Math with Pre-overflow Check
O(log⁡x)O(\log x)O(logx)O(1)O(1)O(1)Strictly avoids any intermediate overflow, satisfying the no-64-bit constraintSlightly more complex overflow logic to follow

The optimal approach for this problem is Solution 3: Math with Pre-overflow Check, which achieves O(log⁡x)O(\log x)O(logx) time complexity and O(1)O(1)O(1) space complexity. By verifying the 32-bit bounds before each digit is appended, it fully satisfies the problem's constraint of not using 64-bit integers while remaining memory-efficient.


Previous1. Two Sum
Next9. Palindrome Number
Related posts
  • 9. Palindrome NumberEasy
  • 13. Roman to IntegerEasy
  • 70. Climbing StairsEasy
  • 509. Fibonacci NumberEasy
On this page
13 lines
1class Solution:
2 def reverse(self, x: int) -> int:
3 # Determine the sign of x and work with its absolute value
4 sign = 1 if x >= 0 else -1
5
6 # Reverse the absolute value of x and apply the original sign
7 reverse_x = int(str(abs(x))[::-1]) * sign
8
9 # Check if the reversed integer is within the 32-bit signed integer range
10 if reverse_x < -2**31 or reverse_x > 2**31 - 1:
11 return 0
12
13 return reverse_x
19 lines
1class Solution:
2 def reverse(self, x: int) -> int:
3 # Determine the sign of x and work with its absolute value
4 sign = 1 if x >= 0 else -1
5
6 x = abs(x) # Work with the absolute value of x for reversal
7 reversed_x = 0
8
9 while x != 0:
10 digit = x % 10 # Get the last digit
11 reversed_x = reversed_x * 10 + digit # Append the digit to the reversed number
12 x //= 10 # Remove the last digit from x
13
14 # Check if the reversed number is within the 32-bit signed integer range
15 if reversed_x < -2**31 or reversed_x > 2**31 - 1:
16 return 0
17
18 # Apply the original sign to the reversed number
19 return reversed_x * sign
29 lines
1class Solution:
2 def reverse(self, x: int) -> int:
3 MAX_INT = 2**31 - 1 # 2147483647
4 MIN_INT = -(2**31) # -2147483648
5
6 sign = 1 if x >= 0 else -1
7 x = abs(x)
8
9 reversed_x = 0
10
11 while x != 0:
12 digit = x % 10 # Get the last digit of x
13 x //= 10 # Remove the last digit from x
14
15 # Check for overflow before actually adding the digit to reversed_x
16 if reversed_x > MAX_INT // 10 or (
17 reversed_x == MAX_INT // 10 and digit > MAX_INT % 10
18 ):
19 return 0
20 if reversed_x < MIN_INT // 10 or (
21 reversed_x == MIN_INT // 10 and digit < MIN_INT % 10
22 ):
23 return 0
24
25 # Update reversed_x with the new digit
26 reversed_x = reversed_x * 10 + digit
27
28 # Return the reversed integer with the original sign
29 return reversed_x * sign