Leetcode 2119. A Number After a Double Reversal
Given an integer num
, return true
if num
can be transformed into num
after two reversals, else return false
.
num
?
num
can be any integer within typical bounds for such problems (e.g., within the range of a 32-bit signed integer: -2^31 to 2^31-1).num
be zero?
true
because “00” after reversal is still “0”.The double reversal operation consists of:
From practical observation:
num
does not contain trailing zeroes, double reversal will return the original number.num
has trailing zeroes, double reversal will remove these trailing zeroes causing the number to change.In essence:
0
itself will remain the same after two reversals.public class Solution {
public boolean isSameAfterReversals(int num) {
// A number without trailing zeros or is zero itself remains unchanged after double reversal
return num == 0 || num % 10 != 0;
}
}
true
since 0 remains 0 after any number of reversals.num
is non-zero, num
doesn’t have trailing zeros and remains unchanged post double reversal.Any number having trailing zeroes will fail this condition and return false
. This assures that any potential input is handled efficiently and correctly.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?