Navbar menu

LeetCode Solution - Problem Reverse Integer

  • 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
Solution:

Java:

class Solution {
    public int reverse(int x) {
        int reversed = 0;
        int min = Integer.MIN_VALUE;
        int max = Integer.MAX_VALUE;
        while (x != 0) {
            int lastDigit = x % 10;

            if (reversed > max/10 || (reversed == max/10 && lastDigit > 7)) {
                return 0;
            }

            if (reversed < min/10 || (reversed == min/10 && lastDigit < -8)) {
                return 0;
            }
            
            reversed = reversed * 10 + lastDigit;

            x = x / 10;
        }
        return reversed; 
    }
}
// TC: O(log n), SC: O(1)

/*
min: -2147483648
max: 2147483647
*/
Python3:

class Solution:
    def reverse(self, x: int) -> int:
        if x > 0:  
            a =  int(str(x)[::-1])  
        if x <=0: 
            a = -1 * int(str(x*-1)[::-1])  

        # handle 32 bit overflow  
        mina = -2**31  
        maxa = 2**31 - 1  
        if a not in range(mina, maxa):  
            return 0  
        else:  
            return a

No comments:

Post a Comment