LeetCode 7. Reverse Integer

Description

https://leetcode.com/problems/reverse-integer/

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Explanation

The question is about writing a function to reverse an integer and return the reversed integer.

We make a while loop to perform reverse operation. For each loop, we divide the original number by 10 and add remainder to the result.

In Java, an integer is 32-bit ranging from 2³¹ – 1 to -2³¹. If the input number is -2³¹, it gets overflows after reversingbecause we wouldn’t have positive 2³¹. In that case, reversed * 10 / 10 != reversed, we return 0.

Video Tutorial

Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *