7. Reverse Integer

Reverse Integer (LeetCode 7) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Two Pointers pattern, in the String Reversal subpattern.

How to solve Reverse Integer

Reverse Integer is a String Reversal Using Two Pointers problem, part of Two Pointers. Master string reversal techniques including reverse string, reverse words, and reverse vowels using two pointers.

Work towards the solution in these steps:

  1. Process one decimal digit at a time rather than converting the entire value to a string.
  2. The last digit can be isolated using remainder by 10, and removing that digit can be done with integer division by 10.
  3. Build the answer by shifting the digits already collected one decimal place to the left before appending the next digit.

The reference solution runs in O(log |x|) time and O(1) space.

The Reverse Integer solution and editorial walks through the full approach with reference code.

Problem statement

Implement Solution.reverse, which receives the integer x and reverses the order of its decimal digits. The sign, if any, remains in front of the reversed digits; leading zeroes in the reversed decimal representation are discarded automatically.

A 32-bit signed integer is an integer from -2^31 through 2^31 - 1, inclusive. Return the reversed integer when it lies in that range. If the reversed value lies outside that range, return 0. The calculation must account for the fact that 64-bit integers are unavailable.

Example 1:

Input: x = 12030 Output: 3021 Explanation: Reversing the decimal digits gives 03021, whose integer value is 3021 because leading zeroes are discarded.

Example 2:

Input: x = -804 Output: -408 Explanation: The digits 804 reverse to 408, and the original negative sign remains negative.

Example 3:

Input: x = 2147483641 Output: 1463847412 Explanation: The reversed value is within the 32-bit signed range, so it is returned unchanged as a valid result.

Constraints:

- x is a JSON integer. - -2^31 <= x <= 2^31 - 1. - The input contains exactly one parameter named x. - The result must be returned as a JSON integer.

Topics and companies

Topics: Math, Simulation, Bit Manipulation.

How to practise Reverse Integer on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(log |x|) time and O(1) space.

Open Reverse Integer in the code editor, or read the Reverse Integer editorial for a worked solution with its approach and complexity analysis.

Where Reverse Integer sits in the DSA pattern sheet

Problems related to Reverse Integer

Other problems that use the same String Reversal and Two Pointers techniques:

Reverse Integer FAQ

What is the LeetCode number of Reverse Integer?

Reverse Integer is LeetCode problem 7, rated Medium.

What pattern is used to solve Reverse Integer?

Reverse Integer is a String Reversal Using Two Pointers problem. It belongs to the Two Pointers pattern, in the String Reversal subpattern.

What is the time complexity of Reverse Integer?

The reference solution runs in O(log |x|) time and O(1) space.

Preparing this workspace.