3783. Mirror Distance of an Integer
UnknownView on LeetCode
Time: O(log n)
Space: O(1)
Problem Overview
Mirror Distance of an Integer (Unknown) asks you to solve a structured algorithmic task. Reverse the digits of n by repeatedly extracting the last digit.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Reverse the digits of n by repeatedly extracting the last digit.
The mirror distance is simply the absolute difference between n and its digit-reverse.
No extra space is needed beyond two integers.
3783.cs
C#
// Approach: Reverse the digits of n by repeatedly extracting the last digit.
// The mirror distance is simply the absolute difference between n and its digit-reverse.
// No extra space is needed beyond two integers.
// Time: O(log n) Space: O(1)
public class Solution
{
public int MirrorDistance(int n)
{
return Math.Abs(n - Reverse(n));
}
private int Reverse(int x)
{
int y = 0;
for (; x > 0; x /= 10)
y = y * 10 + x % 10;
return y;
}
}Was this solution helpful?