{x}
blog image

A Number After a Double Reversal

Reversing an integer means to reverse all its digits.

  • For example, reversing 2021 gives 1202. Reversing 12300 gives 321 as the leading zeros are not retained.

Given an integer num, reverse num to get reversed1, then reverse reversed1 to get reversed2. Return true if reversed2 equals num. Otherwise return false.

 

Example 1:

Input: num = 526
Output: true
Explanation: Reverse num to get 625, then reverse 625 to get 526, which equals num.

Example 2:

Input: num = 1800
Output: false
Explanation: Reverse num to get 81, then reverse 81 to get 18, which does not equal num.

Example 3:

Input: num = 0
Output: true
Explanation: Reverse num to get 0, then reverse 0 to get 0, which equals num.

 

Constraints:

  • 0 <= num <= 106

Solution Explanation:

This problem asks whether reversing an integer twice results in the original integer. The core observation is that only numbers ending in zero will change after a double reversal. Let's break it down:

Why numbers ending in zero change:

Consider the number 120. Reversing it gives 21. Reversing 21 gives 12. The original number is not recovered. This is because the trailing zeros are lost during the first reversal, and cannot be regained.

Why other numbers remain the same:

Consider the number 123. Reversing it gives 321. Reversing 321 gives 123. The original number is retained. This holds true for any number that does not end in zero. The reversal process is symmetric for numbers without trailing zeros.

Mathematical Solution:

The most efficient solution leverages this observation. We simply check if the input number is 0 or if the remainder when divided by 10 (the last digit) is not 0. If either condition is true, then the number will remain unchanged after a double reversal.

Time and Space Complexity:

  • Time Complexity: O(1). The solution involves a single modulo operation and a comparison, which takes constant time regardless of the input number's size.
  • Space Complexity: O(1). The solution uses only a few variables to store the input and the result of the modulo operation, requiring constant space.

Code Implementation (with explanations):

The code in various languages is incredibly concise due to the efficiency of the mathematical observation:

Python:

class Solution:
    def isSameAfterReversals(self, num: int) -> bool:
        return num == 0 or num % 10 != 0
  • num == 0: Handles the base case where the input is 0. Reversing 0 twice still results in 0.
  • num % 10 != 0: Checks if the last digit is non-zero. If it is, the number will remain the same after the double reversal.

Java:

class Solution {
    public boolean isSameAfterReversals(int num) {
        return num == 0 || num % 10 != 0;
    }
}

Identical logic to the Python solution.

C++:

class Solution {
public:
    bool isSameAfterReversals(int num) {
        return num == 0 || num % 10 != 0;
    }
};

Again, the same concise implementation.

Go:

func isSameAfterReversals(num int) bool {
	return num == 0 || num%10 != 0
}

Same logic, showcasing Go's brevity.

TypeScript:

function isSameAfterReversals(num: number): boolean {
    return num === 0 || num % 10 !== 0;
}

TypeScript's implementation mirrors the other languages.

Rust:

impl Solution {
    pub fn is_same_after_reversals(num: i32) -> bool {
        num == 0 || num % 10 != 0
    }
}

Rust's implementation is also straightforward and efficient.

JavaScript:

/**
 * @param {number} num
 * @return {boolean}
 */
var isSameAfterReversals = function (num) {
    return num === 0 || num % 10 !== 0;
};

The JavaScript code is functionally equivalent to the other examples.

All the code snippets follow the same core logic: a simple check for the base case of 0 and whether the last digit is non-zero. This directly addresses the problem's core mathematical property, leading to highly optimized solutions.