LeetCode: Reverse Integer With TypeScript
Oct 24, 2022 · 2 min readIn this post, we’re going to solve LeetCode’s “Reverse Integer” using TypeScript.
Problem
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).
Example 1:
Example 2:
Example 3:
Constraints:
Approach
We can simply convert the number to string, split it into array of characters, reverse the array, then join them back together. After that we have to multiply the result with -1 if the input is negative.
But the method above is not really efficient as we’re doing multiple array’s O(n)
operation.
We can also solve this problem with simple arithmetic. First, it’s important to understand this concept:
- You can add a digit to a number by multiplying the number with 10 and adding the digit to the number.
- You can remove the last digit of a number by dividing with 10 and removing the fractional value.
Now, this is how we are going to solve it:
- First, convert the number to positive because it’s easier to work with.
- Create a variable to store our result.
- Run a loop until input number is equal to 0.
- We append the last digit of the input number to our result variable.
- We remove the last digit from the input number (the input will eventually become 0 this way).
- Multiply the result with -1 if the input number is negative.
- Check if the result exceed the maximum size of signed 32-bit integer, and return the result.
The time complexity for this approach is O(log(x))
. Because the complexity depends on the number of digits in x
, and it can roughly be represented with log(x)
.
Solution
Converting To String
Using Arithmetic
Wrap Up
That’s it for LeetCode’s “Reverse Integer” 🎉.
You can also find the code on my Github.