Difficulty: 🟢 Easy
Reversing an integer means to reverse all its digits.
- For example, reversing
2021
gives1202
. Reversing12300
gives321
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.
0 <= num <= 10^6
class Solution:
def isSameAfterReversals(self, num: int) -> bool:
def reverse(number):
result = 0
while number:
result = result * 10 + number % 10
number //= 10
return result
return reverse(reverse(num)) == num
The given solution solves the problem by defining a helper function reverse
that takes a number as input and reverses its digits. It then checks if the final reversed number is equal to the original number and returns True
if they are equal, and False
otherwise.
The algorithm follows these steps:
- Define a helper function
reverse
that takes a number as input and reverses its digits. This function uses a variableresult
initialized to 0. - Inside the
reverse
function, enter a while loop that continues as long as the input number is non-zero. - Inside the while loop, calculate the next digit of the reversed number by using the expression
result = result * 10 + number % 10
, wherenumber % 10
gives the last digit of the number andresult * 10
shifts the existing digits to the left. - Divide the input number by 10 using the floor division operator
number //= 10
to remove the last digit. - Repeat steps 3-4 until the input number becomes zero.
- After defining the
reverse
function, return the result of the expressionreverse(reverse(num)) == num
, which checks if the final reversed number is equal to the original number.
The time complexity of the algorithm is O(D), where D is the number of digits in the input number. This is because the algorithm reverses the digits of the number twice using the reverse
function, which takes O(D) time.
The space complexity of the algorithm is O(1) because it uses a constant amount of additional space to store the result
variable.
The given solution checks if an integer is equal to the result of reversing it twice. The algorithm reverses the digits of the number using the reverse
function and compares the final reversed number with the original number. The algorithm has a time complexity of O(D) and a space complexity of O(1), where D is the number of digits in the input number.
class Solution:
def isSameAfterReversals(self, num: int) -> bool:
reversed_num = num
while reversed_num != 0 and reversed_num % 10 == 0:
reversed_num //= 10
return num == reversed_num
The given solution approach reverses the number num
and checks if the reversed number is equal to the original number.
The algorithm follows these steps:
- Initialize
reversed_num
as the same value asnum
. - While
reversed_num
is not zero and the last digit ofreversed_num
is zero, dividereversed_num
by 10 to remove trailing zeros. - Check if
num
is equal toreversed_num
. If they are equal, returnTrue
; otherwise, returnFalse
.
The time complexity of the algorithm is O(log n), where n is the value of num
. In the worst case, the number of digits in num
determines the number of iterations required to remove trailing zeros.
The space complexity of the algorithm is O(1) because it uses a constant amount of additional space.
The given solution reverses an integer num
and checks if the reversed number is equal to the original number. It handles cases where there are trailing zeros by removing them before the comparison. The algorithm has a time complexity of O(log n) and a space complexity of O(1).
NB: If you want to get community points please suggest solutions in other languages as merge requests.