Skip to main content

A Number After a Double Reversal

LeetCode 2238 | Difficulty: Easy​

Easy

Problem Description​

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 <= 10^6`

Topics: Math


Approach​

Mathematical​

Look for mathematical patterns or formulas. Consider: modular arithmetic, GCD/LCM, prime factorization, combinatorics, or geometric properties.

When to use

Problems with clear mathematical structure, counting, number properties.


Solutions​

Solution 1: C# (Best: 36 ms)​

MetricValue
Runtime36 ms
Memory28.6 MB
Date2021-12-27
Solution
public class Solution {
public bool IsSameAfterReversals(int num) {
string str = Convert.ToString(num);
int lo = 0, hi = str.Length - 1;
if(num == 0 ) return true;
if(hi>= 0 && str[hi] == '0') return false;

return true;
}
}

Complexity Analysis​

ApproachTimeSpace
Solution$O(n)$$O(1) to O(n)$

Interview Tips​

Key Points
  • Start by clarifying edge cases: empty input, single element, all duplicates.
  • LeetCode provides 1 hint(s) for this problem β€” try solving without them first.
πŸ’‘ Hints

Hint 1: Other than the number 0 itself, any number that ends with 0 would lose some digits permanently when reversed.