Welcome to Subscribe On Youtube

2442. Count Number of Distinct Integers After Reverse Operations

Description

You are given an array nums consisting of positive integers.

You have to take each integer in the array, reverse its digits, and add it to the end of the array. You should apply this operation to the original integers in nums.

Return the number of distinct integers in the final array.

 

Example 1:

Input: nums = [1,13,10,12,31]
Output: 6
Explanation: After including the reverse of each number, the resulting array is [1,13,10,12,31,1,31,1,21,13].
The reversed integers that were added to the end of the array are underlined. Note that for the integer 10, after reversing it, it becomes 01 which is just 1.
The number of distinct integers in this array is 6 (The numbers 1, 10, 12, 13, 21, and 31).

Example 2:

Input: nums = [2,2,2]
Output: 1
Explanation: After including the reverse of each number, the resulting array is [2,2,2,2,2,2].
The number of distinct integers in this array is 1 (The number 2).

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 106

Solutions

Solution 1: Hash Table

First, we use a hash table to record all integers in the array. Then, we traverse each integer in the array, reverse it, and add the reversed integer to the hash table. Finally, we return the size of the hash table.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.

  • class Solution {
        public int countDistinctIntegers(int[] nums) {
            Set<Integer> s = new HashSet<>();
            for (int x : nums) {
                s.add(x);
            }
            for (int x : nums) {
                int y = 0;
                while (x > 0) {
                    y = y * 10 + x % 10;
                    x /= 10;
                }
                s.add(y);
            }
            return s.size();
        }
    }
    
  • class Solution {
    public:
        int countDistinctIntegers(vector<int>& nums) {
            unordered_set<int> s(nums.begin(), nums.end());
            for (int x : nums) {
                int y = 0;
                while (x) {
                    y = y * 10 + x % 10;
                    x /= 10;
                }
                s.insert(y);
            }
            return s.size();
        }
    };
    
  • class Solution:
        def countDistinctIntegers(self, nums: List[int]) -> int:
            s = set(nums)
            for x in nums:
                y = int(str(x)[::-1])
                s.add(y)
            return len(s)
    
    
  • func countDistinctIntegers(nums []int) int {
    	s := map[int]struct{}{}
    	for _, x := range nums {
    		s[x] = struct{}{}
    	}
    	for _, x := range nums {
    		y := 0
    		for x > 0 {
    			y = y*10 + x%10
    			x /= 10
    		}
    		s[y] = struct{}{}
    	}
    	return len(s)
    }
    
  • function countDistinctIntegers(nums: number[]): number {
        const n = nums.length;
        for (let i = 0; i < n; i++) {
            nums.push(Number([...(nums[i] + '')].reverse().join('')));
        }
        return new Set(nums).size;
    }
    
    
  • use std::collections::HashSet;
    impl Solution {
        pub fn count_distinct_integers(nums: Vec<i32>) -> i32 {
            let mut set = HashSet::new();
            for i in 0..nums.len() {
                let mut num = nums[i];
                set.insert(num);
                set.insert({
                    let mut item = 0;
                    while num > 0 {
                        item = item * 10 + (num % 10);
                        num /= 10;
                    }
                    item
                });
            }
            set.len() as i32
        }
    }
    
    

All Problems

All Solutions