Skip to content
LC-2442 Medium LeetCode

2442. Count Number of Distinct Integers After Reverse Operations

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 80% Topics: Array, Hash Table, Math, Counting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogr), r = max(nums)
# Space: O(n)

# hash table   
class Solution(object):
    def countDistinctIntegers(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        def reverse(n):
            result = 0
            while n:
                result = result*10 + n%10
                n //= 10
            return result

        return len({y for x in nums for y in (x, reverse(x))})


# Time:  O(nlogr), r = max(nums)
# Space: O(n)
# hash table   
class Solution2(object):
    def countDistinctIntegers(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return len({y for x in nums for y in (x, int(str(x)[::-1]))})

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions