Posts Reverse Integer
Post
Cancel

Reverse Integer

This question is part of NeetCode150 series.

Problem Description

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).

leetcode

Solution

NeetCode YouTube

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {

    public int reverse(int n) {

        int ans = 0;
        
        //while N is not 0, get the last digit and add it to ans while checking that the current value does not overflow
        while(n != 0){

            //last digit
            int d = n%10;
            //chop off the last digit from N
            n = n/10;

            //if the current ans is more than MAX/10 or current ans is equal to MAX && last digit is more than the digit in MAX_VALUE, return 0
            //similarly for min value
            if(ans > Integer.MAX_VALUE/10 || (ans == Integer.MAX_VALUE/10 && d > Integer.MAX_VALUE%10)) return 0;
            if(ans < Integer.MIN_VALUE/10 || (ans == Integer.MIN_VALUE/10 && d < Integer.MIN_VALUE%10)) return 0;

            ans = (ans * 10) + d;

        }

        return ans;

    }

}
This post is licensed under CC BY 4.0 by the author.