AlgoMaster Logo

Reverse Integer

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. String Manipulation

This simple approach involves converting the integer to a string, reversing it and converting it back to an integer. While naive, it's straightforward to implement and understand.

Code:

2. Mathematical Reverse

Instead of using built-in string manipulation, this approach repeatedly divides the input by 10 to extract digits and form the reversed integer.

Code:

3. Optimized Mathematical Reverse with Overflow Check

Handling overflow is crucial because the problem constraints specify that the reversed integer must fit within a 32-bit signed integer range. This can be efficiently achieved by checking possible overflow conditions before updating the result in each iteration.

Code: