Skip to content

Latest commit

 

History

History
109 lines (73 loc) · 4.61 KB

005.md

File metadata and controls

109 lines (73 loc) · 4.61 KB

Difficulty: 🟢 Easy

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.

Examples:

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

Solutions

O(n) solution

Python3

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:

  1. Define a helper function reverse that takes a number as input and reverses its digits. This function uses a variable result initialized to 0.
  2. Inside the reverse function, enter a while loop that continues as long as the input number is non-zero.
  3. Inside the while loop, calculate the next digit of the reversed number by using the expression result = result * 10 + number % 10, where number % 10 gives the last digit of the number and result * 10 shifts the existing digits to the left.
  4. Divide the input number by 10 using the floor division operator number //= 10 to remove the last digit.
  5. Repeat steps 3-4 until the input number becomes zero.
  6. After defining the reverse function, return the result of the expression reverse(reverse(num)) == num, which checks if the final reversed number is equal to the original number.

Complexity Analysis

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.

Summary

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.

O(log n) solution

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:

  1. Initialize reversed_num as the same value as num.
  2. While reversed_num is not zero and the last digit of reversed_num is zero, divide reversed_num by 10 to remove trailing zeros.
  3. Check if num is equal to reversed_num. If they are equal, return True; otherwise, return False.

Complexity Analysis

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.

Summary

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.