Press "Enter" to skip to content

Reverse Integer

Ajk 0

Today we are going to be visiting a very simple problem, which need some basic maths though. Given an integer, reverse it and return the reversed integer. Sample runs are: 135 -> 531, -12 -> -21, 0 -> 0.

Like we said before, looks fairly simple. Two notes:
1. We are using Java’s integer “special” division, in which -1/2 = 0 (whereas in other languages such as C, it would return -1, which is actually what rounding down is supposed to give)
2. We are not using auxiliary space! Don’t get fooled by Java’s extensive API and think of solving this by making a String/StringBuilder followed by calling the reverse method. Note that in that case you would use O(n) auxiliary space where n is the number of digits in our integer.

The following two tabs change content below.
If you like one of my posts the best way to support is give it a thumbs up, comment, or share it on social media 🙂