Given an integer num, return whether the value of num after performing a double reversal is the same as num.
num.Essentially, you need to determine if the number remains the same after this double reversal. For example:
123 reversed becomes 321 and 321 reversed becomes 123, so it remains the same.1200 reversed becomes 0021 which is 21, and reversing 21 becomes 12, which is different from the original number.num will be a non-negative integer.The double reversal of any number that does not end in 0 (except for 0 itself) will result in the same number. Therefore, we can conclude that a non-zero number which ends in 0 will not remain the same, while others will.
num == 0, return True.num % 10 == 0 and num != 0, return False.True.The time complexity of this solution is (O(1)) because the checks are simple arithmetic operations and do not depend on the size of the input.
def isSameAfterReversals(num: int) -> bool:
if num == 0:
return True
if num % 10 == 0:
return False
return True
num == 0:
num % 10 == 0 and num != 0:
This method ensures an efficient and clear check for the condition mentioned in the problem statement.
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?