Reverse Integer Leetcode Solution

Difficulty Level Medium
Frequently asked in Adobe Amazon Apple Bloomberg ByteDance Cognizant Facebook Google Infosys Intel JP Morgan Microsoft Oracle Samsung Uber Visa VMware YahooViews 6180

Problem Statement

Reverse Integer LeetCode Solution says that – 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:

Input:

 x = 123

Output:

 321

Example 2:

Input:

 x = -123

Output:

 -321

Example 3:

Input:

 x = 120

Output:

 21

 

Constraints:

  • -231 <= x <= 231 - 1

 

Algorithm:

Idea:

  • In order to solve this question, What we will do in this question, At first we will reverse the given integer by breaking the given Integer and After that, We will check if the reverse integer lies within the range of [-2**31,2**31]. Then we will return the reverse integer else if the reverse integer exceeds the range then we will return 0 and obviously we have to focus on the negative integers also if the number is negative then we will use abs function and do the same and at the time of return, we will return negative symbol with the reverse Integer.

Approach:

  • First, we will make one variable total and copy the given integer in the k variable and one symbol variable that is equal to 1.
  •  Then we will check if number is negative then update number  = abs(number) and update symbol = -1.
  • After that, we will use the while conditioning and within that condition, we break the given integer by making one variable b = x%10 and update total = total*10+ b and will update x = x//10.
  • At last, we will check if total > 2**31 then we will return 0 else return total*symbol.

Image of Reverse Integer Leetcode Solution –

Reverse Integer Leetcode Solution

class Solution:
    def reverse(self, x: int) -> int:
        total = 0
        symbol = 1
        k = x
        
        if k < 0:
            x = abs(x)
            symbol = -1
            
        while(x > 0):
            b = x % 10
            total = total*10 + b
            
            x = x//10
        
        if total > pow(2,31):
            return 0
        
        else:
            return total*symbol
        
class Solution {
    public int reverse(int x) {
        if (x >= -9 && x <= 9) {
            return x;
        }
        if (x == Integer.MIN_VALUE || x == Integer.MAX_VALUE) {
            return 0;
        }

        int symbol = x < 0 ? -1 : 1;
        x = Math.abs(x);
        int total = 0;

        while (x > 0) {
            int digit = x % 10;
            if (total > Integer.MAX_VALUE / 10
                    || (total == Integer.MAX_VALUE / 10 && digit > Integer.MAX_VALUE % 10)) {
                return 0;
            }
            total = total * 10 + digit;
            x /= 10;
        }

        return symbol * total;
    }
}

TIME COMPLEXITY: O(N)

SPACE COMPLEXITY: O(1) , As we haven’t taken any extra space.

SIMILAR QUESTION: https://leetcode.com/problems/string-to-integer-atoi/

– https://leetcode.com/problems/reverse-bits/

https://tutorialcup.com/interview/stack/reverse-a-number-using-stack.htm

 

 

 

Translate »